Write a Java program to show that an abstract class can contain concrete methods along with abstract methods
public class AbstractClassExample { public static void main(String[] args) { Circle circle = new Circle(5); Rectangle rectangle = new Rectangle(4, 6); System.out.println("Circle Area : " + circle.calculateArea()); circle.display(); System.out.println("Rectangle Area : " + rectangle.calculateArea()); rectangle.display(); } } abstract class Shape { abstract double calculateArea();// Abstract method (no body) void display() { System.out.println("This is a shape"); } } class Circle extends Shape { double radius; Circle(double radius) // Constructor { this.radius = radius; } @Override double calculateArea()// Implementation of the abstract method { return Math.PI * radius * radius; } } class Rectangle extends Shape { double length; double width; Rectangle(double length, double width) // Constructor { this.length = length; this.width = width; } @Override double calculateArea()// Implementation of the abstract method { return length * width; } }
Circle Area : 78.53981633974483 This is a shape Rectangle Area : 24.0 This is a shape
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions