Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Inheritance. No Comments on Java program on shape hierarchy

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

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

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

Your email address will not be published. Required fields are marked *