Create a Java program to demonstrate an abstract class with a constant variable
abstract class Shape { static final double PI = 3.14159;// Constant variable for PI abstract double calculateArea();// Abstract method to calculate area (to be implemented by subclasses) } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override double calculateArea()// Implementation of the abstract method to calculate the area of the circle { return PI * radius * radius; } } class Square extends Shape { private double side; public Square(double side) { this.side = side; } @Override double calculateArea()// Implementation of the abstract method to calculate the area of the square { return side * side; } } public class Main { public static void main(String[] args) { Circle circle = new Circle(5); Square square = new Square(4); System.out.println("Area of the circle : " + circle.calculateArea()); System.out.println("Area of the square : " + square.calculateArea()); } }
Area of the circle : 78.53975 Area of the square : 16.0
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions