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.
Home » Programming » Java » Programs » Inheritance » Java program on shape hierarchy
Suryateja Pericherla Categories: Inheritance. No Comments on Java program on shape hierarchy
1
(1)

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

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


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?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory