0% found this document useful (0 votes)
12 views2 pages

Practical 20 - 010858

The document defines an abstract class 'Shape' with an abstract method 'area()' and three subclasses: 'Triangle', 'Rectangle', and 'Circle', each implementing the area calculation. The 'ShapeTest' class contains a main method that creates instances of each shape and prints their areas. This demonstrates polymorphism and method overriding in object-oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Practical 20 - 010858

The document defines an abstract class 'Shape' with an abstract method 'area()' and three subclasses: 'Triangle', 'Rectangle', and 'Circle', each implementing the area calculation. The 'ShapeTest' class contains a main method that creates instances of each shape and prints their areas. This demonstrates polymorphism and method overriding in object-oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

abstract class Shape {

abstract double area();

class Triangle extends Shape {

private double base, height;

public Triangle(double base, double height) {

this.base = base;

this.height = height;

@Override

double area() {

return 0.5 * base * height;

class Rectangle extends Shape {

private double length, width;

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

@Override

double area() {

return length * width;

class Circle extends Shape {


private double radius;

public Circle(double radius) {

this.radius = radius;

@Override

double area() {

return Math.PI * radius * radius;

public class ShapeTest {

public sta)c void main(String[] args) {

Shape triangle = new Triangle(20, 15);

Shape rectangle = new Rectangle(14, 26);

Shape circle = new Circle(7);

System.out.println("Area of Triangle: " + triangle.area());

System.out.println("Area of Rectangle: " + rectangle.area());

System.out.println("Area of Circle: " + circle.area());

You might also like