Lab Description:
In this program we are going to use inheritance with the keyword extends to create six programs that will show the following hierarchy:
We will define the superclass and subclass and apply override method on the subclass.
Code:
package Lab_3; class MobileDevice { private String deviceType = "Mobile Device"; public String getDeviceType() { return deviceType; } public void setDeviceType(String MobileDevice) { this.deviceType = MobileDevice; } }
package Lab_3; class SmartPhone extends MobileDevice { private String deviceType = "Mobile Device -> Smart Phone"; @Override public String getDeviceType() { return deviceType; } public void setDeviceType(String deviceType) { this.deviceType = deviceType; } }
package Lab_3; class iPhone extends SmartPhone { private String deviceType = "Mobile Device -> Smart Phone -> iPhone"; @Override public String getDeviceType() { return deviceType; } public void setDeviceType(String deviceType) { this.deviceType = deviceType; } }
package Lab_3; class Android extends SmartPhone { private String deviceType = "Mobile Device -> Smart Phone -> Android"; @Override public String getDeviceType() { return deviceType; } public void setDeviceType(String deviceType) { this.deviceType = deviceType; } }
package Lab_3; class WindowsPhone extends SmartPhone { private String deviceType = "Mobile Device -> Smart Phone -> WindowsPhone"; @Override public String getDeviceType() { return deviceType; } public void setDeviceType(String deviceType) { this.deviceType = deviceType; } }
package Lab_3; public class MobileDeviceClient { public static void main(String[] args) { // TODO Auto-generated method stub MobileDevice myMobileDevice = new MobileDevice(); SmartPhone mySmartPhone = new SmartPhone(); iPhone myiPhone = new iPhone(); Android myAndroid = new Android(); WindowsPhone myWindowsPhone = new WindowsPhone(); System.out.println(myMobileDevice.getDeviceType()); System.out.println(mySmartPhone.getDeviceType()); System.out.println(myiPhone.getDeviceType()); System.out.println(myAndroid.getDeviceType()); System.out.println(myWindowsPhone.getDeviceType()); } }