Create a class called MobileDevice with a private field deviceType that will be
ID: 3846415 • Letter: C
Question
Create a class called MobileDevice with a private field deviceType that will be initialized to the string “Mobile Device” in the constructor. Include the corresponding setter and getter methods.
Create another class SmartPhone that will inherit MobileDevice, with a private field deviceType initialized to the string “Smart Phone” in the constructor, and override the getter method by returning the string:
“Mobile Device -> Smart Phone”. (Hint: To obtain this result make a call to the super class getter method).
Create three more classes: Android, iPhone, and WindowsPhone that will inherit SmartPhone, have a private field deviceType initialized to the string “Android”, “iPhone”, or “Windows Phones” correspondingly in the constructor, and override the getter method returning the corresponding string
“Mobile Device -> Smart Phone -> iPhone” for iPhone,
“Mobile Device -> Smart Phone -> Android” for Android”, or
“Mobile Device -> Smart Phone -> Windows Phone” for Windows Phone.
Your classes must follow this hierarchy:
====> android
MobileDevicee ==> smartphone =====>iphone
=====>windows
Once you create the classes, run the following client code and report your output.
public static void main(String[] args) {public class MobileDeviceClient {
// 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());
}
}
MobileDevicee ==> smartphone =====>iphone
=====>windows
Once you create the classes, run the following client code and report your output.
Explanation / Answer
public class MobileDeviceClient {
public static void main(String[] args) {
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());
}
}
class MobileDevice {
private String deviceType;
public MobileDevice() {
this.deviceType = "Mobile Device";
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}
class SmartPhone extends MobileDevice {
private String deviceType;
public SmartPhone() {
super();
this.deviceType = "Smart Phone";
}
public String getDeviceType() {
return super.getDeviceType()+"==>"+this.deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}
class IPhone extends SmartPhone {
private String deviceType;
public IPhone() {
super();
this.deviceType = "IPhone";
}
public String getDeviceType() {
return super.getDeviceType()+"==>"+this.deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}
class Android extends SmartPhone {
private String deviceType;
public Android() {
super();
this.deviceType = "Android";
}
public String getDeviceType() {
return super.getDeviceType()+"==>"+this.deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}
class WindowsPhone extends SmartPhone {
private String deviceType;
public WindowsPhone() {
super();
this.deviceType = "WindowsPhone";
}
public String getDeviceType() {
return super.getDeviceType()+"==>"+this.deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.