Java Abstract Classes - Abstract Shape Class with Circle and Triangle Subclasses
Write a Java program to create an abstract class Shape with abstract methods calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that extend the Shape class and implement the respective methods to calculate the area and perimeter of each shape.
In the following program Shape is the abstract base class with two abstract methods: calculateArea() and calculatePerimeter(). The Circle and Triangle classes are subclasses of Shape and provide their own implementations for abstract methods.
Sample Solution:
Java Code:
Output:
Radius of the Circle4.0 Area of the Circle: 50.26548245743669 Perimeter of the Circle: 25.132741228718345 Sides of the Traiangel are: 3.0,4.0,5.0 Area of the Triangle: 6.0 Perimeter of the Triangle: 12.0
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program where the "Triangle" subclass includes a method to determine if it is an equilateral triangle.
- Write a Java program where the "Circle" subclass implements a method to calculate circumference.
- Write a Java program where the "Shape" class includes a method to determine if the shape is 2D or 3D.
- Write a Java program where the "Triangle" subclass adds an attribute for angles and checks if it forms a valid triangle.
Go to:
Java Code Editor:
Contribute your code and comments through Disqus.
PREV : Abstract Animal Class with Lion and Tiger Subclasses.
NEXT : Abstract Bank Account Class with Savings and Current Accounts.
What is the difficulty level of this exercise?