|

Object oriented experiment

class Shape {
    // This method will be overridden by its children, even if it's not abstract
    // and its children have not specified @Override
    void draw() {
        System.out.println("Shape drawn");
    };

    @Override
    protected Object clone() {
        return null;
    }
}

class Rectangle extends Shape {
    void draw() {
        System.out.println("Draw rectangle!");
        super.draw();
    }

    // We can change the return type and access of a overridden method from its
    // parent class! Note that access modifier must be the same or more relaxing
    // like private -> protected -> public, but not the other direction
    @Override
    public Rectangle clone() {
        return null;
    }
}
class Triangle extends Shape {
    void draw() {
        System.out.println("Draw Triangle!");
    }
}
class Circle extends Shape {
    void draw() {
        System.out.println("Draw Circle!");
    }
}
public class AbstractClass {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw();
        s = new Triangle();
        s.draw();
        s = new Rectangle();
        s.draw();
        
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *