Lab 4

Lab Description:

In this lab a Inheritance hierarchy is created for the class “Mobile Device” where “Mobile Device” is the superclass and the subclass “SmartPhone” Inherits from the SuperClass “Mobile Device”. The classes named “Andriod” , “iPhone”, and “WindowsPhone” are subclasses that inherit from there superclass “SmartPhone”. New commands and or syntax that are introduced and applied in this lab are: extends, this , and @Override. The keyword extends allows inheritance from one class to another, @Override tells the program that this method overrides the superclass’s method, and the “this” commands allows the object to reference itself.

Code Source:

package lab4_3640;

public class MobileDevice {

private String deviceType = "Mobile Device";

//set & getter methods
public void setDeviceType() {
}

public String getDeviceType()
{
return this.deviceType;
}

}
********************************************************
package lab4_3640;
//extends used for inheritance
public class SmartPhone extends MobileDevice {

private String SmartPhone = "Mobile Device -> Smart Phone";
//set method
@Override
public void setDeviceType() {
// TODO Auto-generated method stub
this.SmartPhone = "SmartPhone";
}
//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
return this.SmartPhone;
}

}
***********************************************************
package lab4_3640;
//extends used for inheritance
public class Android extends SmartPhone {

private String Android = "Mobile Device -> Smart Phone -> Android";
@Override
public void setDeviceType() {
// TODO Auto-generated method stub
this.Android = "Andriod";
}
//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
return this.Android;
}

}
*****************************************************************
package lab4_3640;
//extends used for inheritance
public class iPhone extends SmartPhone {

private String iPhone = "Mobile Device -> Smart Phone -> iPhone";
@Override
public void setDeviceType() {
// TODO Auto-generated method stub
this.iPhone = "iPhone";
}
//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
return this.iPhone;
}

}
*********************************************************************
package lab4_3640;
//extends used for inheritance
public class WindowsPhone extends SmartPhone {

private String WindowsPhone = "Mobile Device -> Smart Phone -> WindowsPhone";
@Override
public void setDeviceType() {
// TODO Auto-generated method stub
this.WindowsPhone = "WindowsPhone";
}
//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
return this.WindowsPhone;
}

}
***************************************************************************************
package lab4_3640;

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

ScreenShots: