Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Hierarchy {
- public static void main(String[] args) {
- // the ultimate superclass Object. It has 4 methods
- Object a = new Object();
- Object z = new Object();
- System.out.println(a);
- System.out.println(z);
- a.getClass();
- a.toString();
- a.hashCode();
- a.equals(z);
- System.out.println(a);
- System.out.println();
- // Object a is changed to a Shape
- //a= new Shape();
- //System.out.println(a);
- //System.out.println("-----------------------");
- // A new shape b
- //Shape b = new Shape(true, "red");
- //System.out.println(b);
- //System.out.println("-----------------------");
- // A new parallelogram c
- //Parallelogram c = new Parallelogram(true, "blue", 5, 9);
- //System.out.println(c);
- //System.out.println("-----------------------");
- // A new parallelogram d
- //Parallelogram d = new Parallelogram(true, "yellow", 6,8); // Note that although we assigned a color of yellow,
- // the constructor made it white since it is not filled
- //System.out.println(d);
- //System.out.println("-----------------------");
- // A new parallelogram made from the Object superclass
- //Object e = new Parallelogram(true, "red", 1,1);
- //System.out.println(e);
- //System.out.println("-----------------------");
- //e = a; // Change "Object e" to "Shape e" and note that "a" cannot be assigned to "e" because "e" is subclass of "a"
- //System.out.println(e);
- //System.out.println("-----------------------");
- //a = null;
- //System.out.println(a);
- //System.out.println();
- //Parallelogram x = new Parallelogram();
- //System.out.println(x);
- }
- }
- class Shape{
- private boolean filled;
- private String color;
- public Shape(){
- System.out.println("I am a shape");
- color = "white";
- }
- public Shape(boolean f, String c){
- filled = f;
- if (filled)
- color = c;
- else
- color= "white";
- System.out.println("I am a shape");
- }
- @Override
- public String toString(){
- String message;
- if (filled == false)
- message = "is not filled. The background is "+white;
- else
- message = "is filled with "+ color;
- return "The shape "+ message;
- }
- }class Parallelogram extends Shape{
- int height;
- int width;
- public Parallelogram(){
- System.out.println("I am a parallelogram");
- }
- public Parallelogram(boolean f, String c, int h, int w){
- super(f,c);
- height = h;
- width = w;
- System.out.println("I am a parallelogram");
- }
- @Override
- public String toString(){
- return super.toString()+"\n The parallelogram has height of "+height + " and width of "+ width;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement