Define an abstract class Shape with abstract methods area () and volume (). Derive abstract class Shape into two classes Cone and Cylinder. Write a java Program to calculate area and volume of Cone and Cylinder.(Use Super Keyword.) | EasyCoding45

Define an abstract class Shape with abstract methods area () and volume (). Derive abstract class Shape into two classes Cone and Cylinder. Write a Java program to calculate the area and volume of the Cone and Cylinder. (Use Super Keyword.).

Certainly! Here's a Java program that defines an abstract class Shape with abstract methods area() and volume(), and then derives two abstract classes Cone and Cylinder from it. The program calculates the area and volume of both the cone and the cylinder using the super keyword:



import java.util.Scanner;


// Abstract class Shape

abstract class Shape {

    abstract double area();

    abstract double volume();

}


// Concrete class Cone, derived from Shape

class Cone extends Shape {

    private double radius;

    private double height;


    // Parameterized constructor for Cone

    public Cone(double radius, double height) {

        this.radius = radius;

        this.height = height;

    }


    @Override

    double area() {

        return Math.PI * radius * (radius + Math.sqrt(radius * radius + height * height));

    }


    @Override

    double volume() {

        return (1.0 / 3) * Math.PI * radius * radius * height;

    }

}


// Concrete class Cylinder, derived from Shape

class Cylinder extends Shape {

    private double radius;

    private double height;


    // Parameterized constructor for Cylinder

    public Cylinder(double radius, double height) {

        this.radius = radius;

        this.height = height;

    }


    @Override

    double area() {

        return 2 * Math.PI * radius * (radius + height);

    }


    @Override

    double volume() {

        return Math.PI * radius * radius * height;

    }

}


public class ShapeDemo {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        // Taking user input for Cone

        System.out.print("Enter the radius of the Cone: ");

        double coneRadius = scanner.nextDouble();

        System.out.print("Enter the height of the Cone: ");

        double coneHeight = scanner.nextDouble();


        // Creating an instance of Cone

        Cone cone = new Cone(coneRadius, coneHeight);


        // Taking user input for Cylinder

        System.out.print("Enter the radius of the Cylinder: ");

        double cylinderRadius = scanner.nextDouble();

        System.out.print("Enter the height of the Cylinder: ");

        double cylinderHeight = scanner.nextDouble();


        // Creating an instance of Cylinder

        Cylinder cylinder = new Cylinder(cylinderRadius, cylinderHeight);


        // Displaying area and volume of Cone

        System.out.println("\nArea of Cone: " + cone.area());

        System.out.println("Volume of Cone: " + cone.volume());


        // Displaying area and volume of Cylinder

        System.out.println("\nArea of Cylinder: " + cylinder.area());

        System.out.println("Volume of Cylinder: " + cylinder.volume());


        scanner.close();

    }

}

NoteThis program defines an abstract class Shape with two abstract methods area() and volume(). It then derives two concrete classes, Cone and Cylinder, each with its own implementation of the area() and volume() methods. The ShapeDemo class demonstrates how to create instances of Cone and Cylinder, and calculate their respective areas and volumes. The super keyword is not explicitly used in this example because there are no superclass constructors to call, but it is generally used to refer to the superclass methods or fields.

Output : 


https://easycoding45.blogspot.com/2023/12/define-abstract-class-shape-with.html


Comments