CET 3640

Here are the source codes for each class if this exercise :

 

Class 1 Source Code

//Joel Pena

//Section: W380

//10/20/2018

public class MobileDevice {

private String  deviceType;

public MobileDevice(){

deviceType= “Mobile Device”;

 

}

public void setDeviceType(){

return;

}

public String getDeviceType(){

return deviceType;

}

}

 

 

 

Class 2 Source Code

//Joel Pena

//Section: W380

//10/20/2018

public class SmartPhone extends MobileDevice{

private String deviceType;

public SmartPhone(){

deviceType= “Smart Phone”;

setDeviceType();

}

 

public String getDeviceType(){

deviceType= super.getDeviceType() + “–>”+ deviceType;

return deviceType;

}

}

 

Class 3 Source Code

 

//Joel Pena

//Section: W380

//10/20/2018

public class IPhone extends SmartPhone {

private String deviceType;

public IPhone(){

deviceType= “iphone”;

setDeviceType();

 

}

 

public String getDeviceType(){

deviceType= super.getDeviceType() + “—>”+ deviceType;

return deviceType;

}

}

 

Class 4 Source Code

//Joel Pena

//Section: W380

//10/20/2018

public class Android extends SmartPhone {

private String deviceType;

public Android(){

deviceType= “Android”;

setDeviceType();

 

 

 

}

public String getDeviceType(){

deviceType= super.getDeviceType() + “–>”+deviceType;

return deviceType;

}

}

 

Class 5 Source Code

//Joel Pena

//Section: W380

//10/20/2018

 

public class WindowsPhone extends SmartPhone {

privateString deviceType;

public WindowsPhone(){

deviceType= “Windows Phone”;

setDeviceType();

}

public String getDeviceType(){

deviceType= super.getDeviceType() + “–>”+ deviceType;

return deviceType;

}

}

 

 

 

 

 

Description: What I have done is I created a inheritance hierachy of mobile devices. The first class I created for my hierachy is named MobileDevice. Inside of the class I added a  private field named deviceType which is initialized to the string “Mobile Device” inside the constructor that I created for the class along with a set and get method. Then I created a second class named SmartPhone That Inherits the methods from the MobileDevice class which is the superclass of the class SmartPhone by using the “extends” command and adding the class that will be extended (MobileDevice class) as shown above. I added the private field deviceType and assigned it to the string “Smart Phone”. I used the setDeviceType() method of the MobileDevice class to set the device type of the new class.  I overrided the get method of the MobileDevice class inside of the SmartPhone class by giving the deviceType field a new assignment and inside that assignment I accessed  the method of the super class to get the device type of the super class by using “super.getDeviceType()” . getDeviceType() is the name of the method and added the new deviceType field which is “Smart Phone”. After overriding the getDeviceType() method inside the SmartPhone class I created three more classes named IPhone , Android and WindowsPhone. In each class  I used the SmartPhone class as the superclass. Each containing the deviceType field to assign the new device type. In each class I used the set and get methods of the SmartPhone super class which is the same methods as the MobileDevice classand overrided the get method of the SmartPhone class to print the superclass device type and the new device type.

 

After creating all classes I ran the following client code and got the following output:

 

public classMobileDeviceClient {

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());

}

}

 

Output

 

Mobile Device Inheritance Hierachy

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Â