Lab5

The objective of this lab is to learn about inheritance in Java as well as the proper usage of abstract classes and methods. The program has to be able to follow the hierarchy below through the use of inheritance.

package v22;

public class Android extends Smartphone{

    private String myAndroid = "Android";

    public void setDeviceType(){
        this.myAndroid = "Android";
    }

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

    @Override
    public String toString(){
        return this.myAndroid;
    }

}
package v22;

public class Ipad extends Tablet {

    private String myIpad = "Ipad";

    public void setDeviceType(){
        this.myIpad = "Ipad";
    }

    public String getDeviceType(){
        return this.myIpad;
    }
    @Override
    public String toString(){
        return this.myIpad;
    }

}
package v22;

public abstract class MobileDevice {

    public String mobileDevice = "Mobile Device";

    public abstract void setDeviceType();

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

    public String toString(){
        return this.mobileDevice;
    }
}
package v22;

public class MobileDeviceClient {

    public static void main (String[] args){

        MobileDevice[] mobileDevices = new MobileDevice[4];

        mobileDevices[0] = new Tablet();
        mobileDevices[1] = new Smartphone();
        mobileDevices[2] = new Ipad();
        mobileDevices[3] = new Android();

        for (MobileDevice m: mobileDevices){
            System.out.println("This device is: " + m.getDeviceType());
        }
    }
}
package v22;

public class Smartphone extends MobileDevice {

    private String mySmartphone = "Smartphone";

    public void setDeviceType(){
        this.mySmartphone = "Smartphone";
    }

    public String getDeviceType(){
        return this.mySmartphone;
    }
    @Override
    public String toString(){
        return this.mySmartphone;
    }

}
package v22;

public class Tablet extends MobileDevice {

    private String myTablet = "Tablet";

    public void setDeviceType(){
        this.myTablet = "Tablet";
    }

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

    @Override
    public String toString(){
        return this.myTablet;
    }

}

Screen