Program to demonstrate concept of abstract class in java

abstract class AbstractShape {
    protected double side1;
    protected double side2;
   
    AbstractShape() {
    }

    public void typeOfShape(String type) {
        System.out.println("Shape is " + type);
    }
   
    public abstract void area();
}

class Square extends AbstractShape {
    public Square() {
        side1 = side2 = 0.0;
    }
   
    public Square(double side) {
        side1 = side2 = side;
    }
   
    public void area() {
        System.out.println("Area of square is " + (side1 * side2));
    }
}

class Rectangle extends AbstractShape {
    public Rectangle() {
        side1 = side2 = 0.0;
    }
   
    public Rectangle(double side1, double side2) {
        this.side1 = side1;
        this.side2 = side2;
    }
   
    public void area() {
        System.out.println("Area of rectangle is " + (side1 * side2));
    }
}

public class AbstractConcept {
    public static void main(String[] args) {
        Square s = new Square(10.0);
        Rectangle r = new Rectangle(2.5, 7.5);
      
        s.area();
        s.typeOfShape("square");
        r.area();
        r.typeOfShape("rectangle");
    }
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments