Write a 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 to print number of sides for differet shapes
abstract class Shape{
 abstract void numberofsides();
}
class Trapezoid extends Shape{
            void numberofsides()
            {
                        System.out.println("Number of sides of Trapeziod is : 4");
            }
}
class Triangle extends Shape
{
            void numberofsides()
            {
                        System.out.println("Number of sides of Triangle is : 3");
            }
}
class Hexagon extends Shape
{
            void numberofsides()
            {
                        System.out.println("Number of sides of Hexagon is : 6");
            }
}
class Sides
{
            public static void main(String args[])
            {
                        Shape s;
                        s=new Trapezoid();
                        s.numberofsides();
                        s=new Triangle();
                        s.numberofsides();
                        s=new Hexagon();
                        s.numberofsides();      
                       
            }
}
 
Output:
 
Number of sides of Trapeziod is : 4
Number of sides of Triangle is : 3

 

Number of sides of Hexagon is : 6

One comment

  1. Write a Java program to create an interface named Shape, that contains an empty method named numberOfSides(). Provide three classes named Trapezoid, Triangle and Hexagon, such that each one of the classes contain only the method numberOfSides(), that contains the number of sides in the given geometric figure.

Leave a Reply

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

Enable Notifications OK No thanks