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 deviceType;
public SmartPhone(){
deviceType = "SmartPhone";//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
deviceType = super.getDevice() +" -> " + deviceType;
return deviceType;
}}
***********************************************************
package lab4_3640;
//extends used for inheritance
public class Android extends SmartPhone {private String deviceType;
public Android(){
deviceType = "Android";//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
deviceType = super.getDevice() +" -> " + deviceType;
return deviceType;
}}
*****************************************************************
package lab4_3640;
//extends used for inheritance
public class iPhone extends SmartPhone {private String deviceType;
public iPhone(){
deviceType = "iPhone";//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
deviceType = super.getDevice() +" -> " + deviceType;
return deviceType;
}}
*********************************************************************
package lab4_3640;
//extends used for inheritance
public class WindowsPhone extends SmartPhone {private String deviceType;
public WindowsPhone(){
deviceType = "WindowsPhone";//getter method which is override to return the string
@Override
public String getDeviceType() {
// TODO Auto-generated method stub
deviceType = super.getDevice() +" -> " + deviceType;
return deviceType;
}}
***************************************************************************************
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());
}
}