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

Write A Java Program To Create Abstract Class Shape and Three Class Named Trapezoid, Triangle and Hexagon

The document defines an abstract Shape class with a method to return the number of sides. It then defines three subclasses (Trapezoid, Triangle, Hexagon) that override the method and return the number of sides for each shape. The main method creates instances of each subclass, stores them in an array, and prints out the name and number of sides for each.

Uploaded by

Diksha Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
279 views2 pages

Write A Java Program To Create Abstract Class Shape and Three Class Named Trapezoid, Triangle and Hexagon

The document defines an abstract Shape class with a method to return the number of sides. It then defines three subclasses (Trapezoid, Triangle, Hexagon) that override the method and return the number of sides for each shape. The main method creates instances of each subclass, stores them in an array, and prints out the name and number of sides for each.

Uploaded by

Diksha Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

WRITE A JAVA PROGRAM TO CREATE ABSTRACT CLASS SHAPE

AND THREE CLASS NAMED TRAPEZOID,TRIANGLE AND


HEXAGON
abstract class Shape
{
abstract int numberOfSides();
}

class Trapezoid extends Shape


{
private static int sides=4;
int numberOfSides()
{
return sides;
}
public String toString()
{
return "Trapezoid";
}
}

class Triangle extends Shape


{
private static int sides=3;
int numberOfSides()
{
return sides;
}
public String toString()
{
return "Triangle";
}
}

class Hexagon extends Shape


{
private static int sides = 6;
int numberOfSides()
{
return sides;
}
public String toString()
{
return "Hexagon";
}
}

public class Shapes


{
public static void main(String args[])
{
Shape[] shapes = new Shape[4];
Trapezoid tp = new Trapezoid();
Triangle tr = new Triangle();
Hexagon hx = new Hexagon();
shapes[0]=tp;
shapes[1]=tr;
shapes[2]=hx;
for(int i=0 ; i<3 ;i++)
{
[Link](shapes[i].toString()+"has
sides:"+shapes[i].numberOfSides());

}
}

OUTPUT:
Trapezoid has sides: 4
Triangle has sides:3
Hexagon has sides:6

You might also like