Method Overriding in Java
Method overriding is one of the importance concept in Java Programming. What is Method Overriding? In Inheritance hierarchy when a child class defines exact same method that is available in parent class then the method is called overridden method. The process of creating overridden method is called method overriding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Shape { void draw() { System.out.println("Draw Shape"); } } class Triangle extends Shape { void draw() { System.out.println("Draw Triangle Shape"); } } class Rectangle extends Shape { void draw() { System.out.println("Draw Rectangle Shape"); } } class Circle extends Shape { void draw() { System.out.println("Draw Circle Shape"); } } public class MyClass { public static void main(String args[]) { Shape shape = new Shape(); shape.draw(); Triangle triangle = new Triangle(); triangle.draw(); Rectangle rectangle = new Rectangle(); rectangle.draw(); Circle circle = new Circle(); circle.draw(); } } |
Result
1 2 3 4 | Draw Shape Draw Triangle Shape Draw Rectangle Shape Draw Circle Shape |
We have created … Read more