CET 3640

Code Objective and Explanation

The objective of this code is to create a class hierarchy where we have a Superclass be represented by subclasses. A subclass is another class that will copy the existing structure of the superclass and can do the same thing it does, but also can be customized. For this hierarchy, we used “Mobiledevice” as the superclass, “Smartphones” as the subclass of Mobiledevice and a list of phone types to be the subclasses of the Smartphone class. To use the superclass in the subclass we had to use the Java command “extends” when creating the subclass, from here alone this class can work without any more code beside a constructor, it will have the same fields, variables, and methods as the superclass. Lastly, part of the reason for extending a class is for making a similar class with small changes, to change parts of the class like methods, we simply create a method in the subclass with the same name as the method in the superclass and just type in whatever statements we want.

Java Code UML Flowchart

This image displays the Hierarchy of the SuperClasses and their SubClasses

UML Flow Chart

Java Source Code
//Main Class
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());
       }
}

//MobileDevice Class

public class MobileDevice {
       private String deviceType;
              public MobileDevice ()
              {
                     this.deviceType= "Mobile Device";
              }
              public String getDeviceType()
              {
                     return this.deviceType ;
              }
              public void setDeviceType (String deviceType)
              {
                     this.deviceType=deviceType;
              }
       }

//SmartPhone Class
public class SmartPhone extends MobileDevice{
       private String deviceType;
       public SmartPhone ()
       {              this.deviceType="SmartPhone";       }       
@Override
              public String getDeviceType()
       {
              return super.getDeviceType() + "->" + deviceType ;
       }



//Android class
public class Android extends SmartPhone {
private String deviceType;      
       public Android()
       {
              this.deviceType="Android";
       }
       public String getDeviceType()
       {
              return super.getDeviceType() + "->" + deviceType;
       }
}
                    
//WindowsPhone Class
public class WindowsPhone extends SmartPhone{
       private String deviceType;
       public WindowsPhone()
       {
              this.deviceType="Windows Phone";
       }
       public String getDeviceType()
       {
              return super.getDeviceType() + "->" + deviceType;
       }
}

//iphone Class
public class iPhone extends SmartPhone {
private String deviceType;
       public iPhone()
       {
              this.deviceType="iPhone";
       }
       public String getDeviceType()
              return super.getDeviceType() + "->" + deviceType;
       }
}