Week8 - Java Program
Week8 - Java Program
Develop a JAVA program to create a class named shape. Create three sub classes namely:
circle, triangle and square, each class has two member functions named draw () and erase
(). Demonstrate polymorphism conceptsby developing suitable methods, defining member
data and main program.
// Main class
public class Main
{
public static void main(String[] args)
{
// Polymorphism: Creating objects of different subclasses using the reference of the
superclass
Shape shape1 = new Circle();
Shape shape2 = new Triangle();
Shape shape3 = new Square();
// Demonstrating polymorphic behavior
shape1.draw(); // Calls draw() method of Circle class
shape1.erase(); // Calls erase() method of Circle class
shape2.draw(); // Calls draw() method of Triangle class
shape2.erase(); // Calls erase() method of Triangle class
shape3.draw(); // Calls draw() method of Square class
shape3.erase(); // Calls erase() method of Square class
}
}
OUTPUT
Drawing a circle
Erasing a circle
Drawing a triangle
Erasing a triangle
Drawing a square
Erasing a square