Lab 4

Lab Description:

The goal of this lab is to create a Java program that use inheritance. Inheritance is a form of software where new class is created by inheriting an existing class. It is like how you inheritance something from your grandfather or grandmother (eyes etc.) . The keyword use in this lab are extends and @override. The program must follow the given hierarchy :

Also the program must be able to run a client code also given to us. The program must use the inheritance method that following the given hierarchy and must run the client code.

Code:

 

package lab_4;

public class MobileDevice {
	private String deviceType;

	public MobileDevice() {

		deviceType = "Mobile Device";
		// TODO Auto-generated constructor stub
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

}
package lab_4;

public class iPhone extends SmartPhone{
	private String deviceType;

	public iPhone () {

		deviceType = "iPhone";
	}

	@Override

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

}
package lab_4;

public class WindowsPhone extends SmartPhone {
	private String deviceType;

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

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

}

package lab_4;

public class SmartPhone extends MobileDevice {

	private String deviceType;
	public SmartPhone() {
		deviceType = "SmartPhone";
	}
	// TODO Auto-generated constructor stub
	public String getDeviceType () {
		deviceType = super.getDeviceType() + " -> " + deviceType;
		return deviceType;

	}

}
package lab_4;

public class Android extends SmartPhone {
	private String deviceType; 

	public Android() {
		deviceType = "Android" ;

	}
	@Override

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

}
package lab_4;


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: