Demonstration of abstract classes in Java.
Program Overview:
- The Geometry Calculation Program is designed to demonstrate object-oriented principles through the calculation of areas for various geometric shapes: circles, triangles, and rectangles. It features an abstract class Shape that defines abstract methods for calculating the areas of a circle and a triangle, along with a concrete method for calculating the area of a rectangle. The Geometry class extends Shape and provides implementations for the abstract methods. The program showcases polymorphism, abstraction, and inheritance in Java.
Features:
- Abstract Shape Class: Serves as a blueprint for geometric shapes with methods to calculate areas.
- Geometry Class: Extends Shape and implements the abstract methods to calculate the areas of a circle and a triangle.
Area Calculations:
- Circle: Utilizes the formula πr².
- Triangle: Utilizes the formula ½ * base * height.
- Rectangle: Utilizes the formula length * width (implemented directly in Shape).
How to Run:
- Ensure Java is installed on your system.
- Save the Shape.java, Geometry.java, and Main.java files in the same directory.
- Compile the program with javac Main.java.
- Run the compiled program with java Main.
- Observe the output, which will display the areas of a circle, triangle, and rectangle with predefined dimensions.
Usage:
- The program is initiated from Main.java, where instances of Geometry are used to calculate and print the areas of a circle, a triangle, and a rectangle.
- To calculate areas of different sizes, modify the parameters in the Main.java file before compiling and running the program.
Implementation Details:
Shape (Abstract Class): Defines the structure for subclasses with two abstract methods (calculateAreaOfCircle and calculateAreaOfTriangle) and one concrete method (calculateAreaOfRectangle). Geometry (Class): Implements the abstract methods from Shape for calculating the areas of a circle and a triangle. Inherits the concrete method for calculating the area of a rectangle directly from Shape.
Purpose:
- This program serves as an educational tool to understand basic concepts of object-oriented programming (OOP) such as inheritance, abstraction, and polymorphism. It provides a practical application of these concepts through simple geometric calculations.
Extending the Program:
- To extend the program, one might consider adding more shapes, such as squares or parallelograms, by defining new abstract methods in Shape and implementing them in Geometry.
- Another extension could involve enhancing the Main class to accept user input for dimensions, allowing for dynamic calculation of areas.