Lab 3

Lab Description
This lab is basically a exercise to use inheritance in Java programming. In this lab, we have to create class called MobileDevice with a private field deviceType that will start as the string “Mobile Device” in the constructor. Then we will create another class called SmartPhone that will inherit MobileDevice, and three more classes, Android, iPhone and WindowsPhone that will inherit Smartphone.
The first thing I did is to create a class called MobileDevice and using String deviceType as a private field in the constructor. Then I created another class called SmartPhone that will inherit MobileDevice using the keyword extends, using the override method, and getter method which makes call to the super class getter method to return the specific string. Then, I created three more classed, Android, iPhone and WindowsPhone. Through using the same method as SmartPhone that inherits MobileDevice, these three classes will be inheriting SmartPhone. After compiling the program, the result should display:
Mobile Device
Mobile Device –> Smart Phone
Mobile Device –> Smart Phone –> iPhone
Mobile Device –> Smart Phone –> Android
Mobile Device –> Smart Phone –> Windows Phone

Program Codes

package lab3;

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

	}

}
package lab3;

public class MobileDevice {

	private String deviceType;

	public MobileDevice(){
		deviceType = "Mobile Device";
	}

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

package lab3;

public class SmartPhone extends MobileDevice

{
	private String deviceType;

	public SmartPhone() {
		deviceType = "Smart Phone";
	}


	@Override
	public String getDeviceType() {
		deviceType = super.getDeviceType() + " --> " + deviceType;
		return deviceType;
	}

}
package lab3;

public class iPhone extends SmartPhone 
{
	private String deviceType;

	public iPhone() {
		deviceType = "iPhone";
	}


	@Override
	public String getDeviceType() {
		deviceType = super.getDeviceType() + " --> " + deviceType;
		return deviceType;
        }

}
package lab3;

public class Android extends SmartPhone 
{
	private String deviceType;

	public Android() {
		deviceType = "Android";
	}


	@Override
	public String getDeviceType() {
		deviceType = super.getDeviceType() + " --> " + deviceType;
		return deviceType;
	}

}
package lab3;

public class WindowsPhone extends SmartPhone
{
	private String deviceType;

	public WindowsPhone() {
		deviceType = "WindowsPhone";
	}


	@Override
	public String getDeviceType() {
		deviceType = super.getDeviceType() + " --> " + deviceType;
		return deviceType;
	}

}

ScreenShots

1

2

3

4

5

6

7