Friday, November 18, 2016

Inheritance and Polymorphism Revisited - Challenge



Start with the UML below.  You should try to implement this UML with a simple code not even looking at the code below.  See if you can successfully interpret the UML.


You should interpret the UML above and write the problem statement this UML solves.

You can compare your code to the code below or use it as a reference if you get stuck.

/** Animal class that is used as the super class with different modifiers to help understand inheritance
* @version 1.0.0.0
* @author Zoltan Szabo
*/
public class Animal{
    public int x=50;
    private int y=60;
    protected int z=70;
    int k=80;
  
    public static int XX=150;
    private static int YY=160;
    protected static int ZZ=170;
    static int KK=180;
  
/** Super class no-arg constuctor*/
   public Animal(){
      System.out.println("This is the superclass (Animal) constructor.");
   }

/** Public method in the superclass
* @return x public field from superclass
*/
   public int func1(){
        return x;  
   }

/** Private method in the superclass
* @return y private field from superclass
*/   private int func2(){
        return y;
   }

/** Protected method in the superclass
* @return z protected field from superclass
*/   protected int func3(){
        return z;
   }

/** Public method in the superclass
* @return k default field from superclass
*/   int func4(){
        return k;
   }
}

/**
*Cat class that is a child of the Animal class inherits public, protected, and default attributes and methods
*@version 1.0.0
*@author Zoltan Szabo
*/ public class Cat extends Animal{
/** SubSubClass1 class no-arg constuctor*/
   public Cat(){
      System.out.println("I\'m a Cat constructor.");
   }
}

/**
*Class that is a child of the Animal class inherits public, protected, and default attributes and methods
*@version 1.0.0
*@author Zoltan Szabo
*/

public class Dog extends Animal{

/** SubClass1class no-arg constuctor*/
   public Dog(){
      System.out.println("I'm a Dog constructor.");
   }
   public void doTrick(){
       System.out.println("I can roll over.");
    }
}

/** This simple application will help you understand polymorphism
* @version 1.0.0
* @author Zoltan Szabo
*/

public class AnimalsDemo{
    final static int INDEX=2;

/** application entry point to test inheritance of modifiers
* @param args as a String array for command argument input
*/

   public static void main(String[] args)   {

      Animal obj[]=new Animal[5];
      obj[0]=new Animal();
      obj[1]=new Dog();
      obj[2]=new Cat();
     /*
      * according to the java language specification, 3rd edition:
      * 6.6.8 Example: private Fields, Methods, and Constructors
      * A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
      * It is not inherited by subclasses.
      */
     //Instantiate one of each objects in an Animal array of objects
     //Display collar color for each animals and the length of the leash where it applies.
   }
}

Now take the initial code and extend the application to implement this UML.

 The output should look like this.



No comments:

Post a Comment