Advertisement
therrontelford

Hierarchy

Feb 2nd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1.  
  2. public class Hierarchy {
  3.  
  4.     public static void main(String[] args) {
  5.        
  6.         // the ultimate superclass Object.  It has 4 methods
  7.         Object a = new Object();
  8.         Object z = new Object();
  9.         System.out.println(a);
  10.         System.out.println(z);
  11.         a.getClass();
  12.         a.toString();
  13.         a.hashCode();
  14.         a.equals(z);
  15.         System.out.println(a);
  16.         System.out.println();
  17.        
  18.         // Object a is changed to a Shape
  19.         //a= new Shape();
  20.         //System.out.println(a);
  21.         //System.out.println("-----------------------");
  22.        
  23.         // A new shape b
  24.         //Shape b = new Shape(true, "red");
  25.         //System.out.println(b);
  26.         //System.out.println("-----------------------");
  27.        
  28.         // A new parallelogram c
  29.         //Parallelogram c = new Parallelogram(true, "blue", 5, 9);
  30.         //System.out.println(c);
  31.         //System.out.println("-----------------------");
  32.        
  33.         // A new parallelogram d
  34.         //Parallelogram d = new Parallelogram(true, "yellow", 6,8);  // Note that although we assigned a color of yellow,
  35.                                                                     // the constructor made it white since it is not filled
  36.         //System.out.println(d);
  37.         //System.out.println("-----------------------");
  38.        
  39.         // A new parallelogram made from the Object superclass
  40.         //Object e = new Parallelogram(true, "red", 1,1);
  41.         //System.out.println(e);
  42.         //System.out.println("-----------------------");
  43.        
  44.         //e = a;  // Change "Object e" to "Shape e" and note that "a" cannot be assigned to "e" because "e" is subclass of "a"
  45.         //System.out.println(e);
  46.         //System.out.println("-----------------------");
  47.         //a = null;
  48.         //System.out.println(a);
  49.         //System.out.println();
  50.        
  51.         //Parallelogram x = new Parallelogram();
  52.         //System.out.println(x);
  53.        
  54.  
  55.     }
  56.  
  57. }
  58. class Shape{
  59.     private boolean filled;
  60.     private String color;
  61.    
  62.     public Shape(){
  63.         System.out.println("I am a shape");
  64.         color = "white";
  65.     }
  66.     public Shape(boolean f, String c){
  67.         filled = f;
  68.         if (filled)
  69.             color = c;
  70.         else
  71.             color= "white";
  72.        
  73.         System.out.println("I am a shape");
  74.     }
  75.     @Override
  76.     public String toString(){
  77.         String message;
  78.         if (filled == false)
  79.             message = "is not filled. The background is "+white;
  80.         else
  81.             message = "is filled with "+ color;
  82.         return "The shape "+ message;
  83.     }
  84.    
  85. }class Parallelogram extends Shape{
  86.     int height;
  87.     int width;
  88.    
  89.     public Parallelogram(){
  90.         System.out.println("I am a parallelogram");
  91.     }
  92.    
  93.     public Parallelogram(boolean f, String c, int h, int w){
  94.         super(f,c);
  95.         height = h;
  96.         width = w;
  97.         System.out.println("I am a parallelogram");
  98.     }
  99.     @Override
  100.     public String toString(){
  101.         return super.toString()+"\n The parallelogram has height of "+height + " and width of "+ width;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement