public class Polimorfisme {
    public static void main(String[] args) {
        
        Shape s1 = new Rectangle("Red", 10, 4);
        ((Rectangle)s1).showRectangle(); 
                
        Rectangle s3 = new Rectangle("Black", 3, 3);
    
        Shape s2 = s3;
        Shape s4 = (Rectangle) s3;
    
        
        Rectangle s5 = new Rectangle("Yellow",2,2);
        Shape s8 = s5;
        Rectangle s9 = (Rectangle) s8;
        
    }
}
public class Shape {
    
    private String color;
    public Shape (String color) {
        this.color = color;
    }
    public void showShape() {
        System.out.println("ERROR ! Dilarang memanggil method ini");
    }
}
public class Rectangle extends Shape {
   private int length=0;
   private int width=0;
   
   public Rectangle(String color, int length, int width) {
      super(color);
      this.length = length;
      this.width = width;
   }
   
   public void showRectangle() {
      System.out.println("Luas Persegi Panjang = "+(length*width));
   }
}

0 komentar:
Post a Comment