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

Java Program 6

The document describes a Java program that defines an abstract class named 'Shape' with an abstract method 'numberOfSides()'. It includes three subclasses: 'Trapezoid', 'Triangle', and 'Hexagon', each implementing the 'numberOfSides()' method to display the number of sides of the respective geometric figures. The main class creates instances of these subclasses and calls the 'numberOfSides()' method to print the results.

Uploaded by

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

Java Program 6

The document describes a Java program that defines an abstract class named 'Shape' with an abstract method 'numberOfSides()'. It includes three subclasses: 'Trapezoid', 'Triangle', and 'Hexagon', each implementing the 'numberOfSides()' method to display the number of sides of the respective geometric figures. The main class creates instances of these subclasses and calls the 'numberOfSides()' method to print the results.

Uploaded by

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

Java Program 6:

Write a java program to create an abstract class named shape that contains an empty
method named number of sides (). Provide three classes named trapezoid, triangle and
Hexagon such that each one of the classes extends the class shape. Each one of the class
contains only the method number of sides () that shows the number of sides in the given
geometrical figures.

// Abstract class
abstract class Shape {
// Abstract method (no body)
abstract void numberOfSides();
}

// Trapezoid class
class Trapezoid extends Shape {
void numberOfSides() {
[Link]("Trapezoid has 4 sides");
}
}

// Triangle class
class Triangle extends Shape {
void numberOfSides() {
[Link]("Triangle has 3 sides");
}
}

// Hexagon class
class Hexagon extends Shape {
void numberOfSides() {
[Link]("Hexagon has 6 sides");
}
}
// Main class
public class Main {
public static void main(String[] args) {

Shape s1 = new Trapezoid();


Shape s2 = new Triangle();
Shape s3 = new Hexagon();

[Link]();
[Link]();
[Link]();
}
}

Output

You might also like