In this article we will learn to implement a Java program on shape hierarchy. The requirements for writing the program is specified below.
Java program to create an abstract class named Shape that contains an empty method named numberOfSides( ).Provide three classes named Trapezoid, Triangle and Hexagon such that each one of the classes extends the class Shape. Each one of the classes contains only the method numberOfSides( ) that shows the number of sides in the given geometrical figures.
Program is as follows:
abstract class Shape
{
abstract void noOfSides();
}
class Trapezoid extends Shape
{
void noOfSides()
{
System.out.println("No.of sides of a trapezoid is: 4");
}
}
class Triangle extends Shape
{
void noOfSides()
{
System.out.println("No.of sides of a triangle is: 3");
}
}
class Hexagon extends Shape
{
void noOfSides()
{
System.out.println("No.of sides of a hexagon is: 6");
}
}
class ShapeDemo
{
public static void main(String args[])
{
Shape s;
s = new Trapezoid();
s.noOfSides();
s = new Triangle();
s.noOfSides();
s = new Hexagon();
s.noOfSides();
}
}
Output for the above program is as follows:
No.of sides of a trapezoid is: 4
No.of sides of a triangle is: 3
No.of sides of a hexagon is: 6
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply