Write a java program to create a class named shape. It should contain 2 methods, draw() and erase() that prints “Drawing Shape” and “Erasing Shape” respectively.

Write a program to create a class named shape. It should contain 2 methods, draw() and erase() that print “Drawing Shape” and “Erasing Shape” respectively.

For this class, create three sub-classes, Circle, Triangle, and Square and each class should override the parent class functions - draw () and erase (). 

The draw() method should print “Drawing Circle”, “Drawing Triangle” and “Drawing Square” respectively.

The erase() method should print “Erasing Circle”, “Erasing Triangle” and “Erasing Square” respectively.

Create objects of Circle, Triangle, and Square in the following way and observe the polymorphic nature of the class by calling draw() and erase() method using each object.

Shape c=new Circle();

Shape t=new Triangle();

Shape s=new Square();



Source Code:

1:  class Shape {  
2:       void draw() {  
3:            System.out.println("Drawing Shape");  
4:       }  
5:       void erase() {  
6:            System.out.println("Erasing Shape");  
7:       }  
8:  }  
9:  class Circle extends Shape {  
10:       @Override  
11:       void draw() {  
12:            System.out.println("Drawing Circle");  
13:       }  
14:       @Override  
15:       void erase() {  
16:            System.out.println("Erasing Circle");  
17:       }  
18:  }  
19:  class Triangle extends Shape {  
20:       @Override  
21:       void draw() {  
22:            System.out.println("Drawing Triangle");  
23:       }  
24:       @Override  
25:       void erase() {  
26:            System.out.println("Erasing Triangle");  
27:       }  
28:  }  
29:  class Square extends Shape {  
30:       @Override  
31:       void draw() {  
32:            System.out.println("Drawing Square");  
33:       }  
34:       @Override  
35:       void erase() {  
36:            System.out.println("Erasing Square");  
37:       }  
38:  }  
39:  public class Solution {  
40:       public static void main(String[] args) {  
41:            // TODO Auto-generated method stub  
42:            Shape c = new Circle();  
43:            Shape t = new Triangle();  
44:            Shape s = new Square();  
45:            c.draw(); c.erase();  
46:            t.draw(); t.erase();  
47:            s.draw(); s.erase();  
48:       }  
49:  }