PREV NEXT
Java Method overriding:
If a method inherited from the superclass requires a different definition other than the definition provided in the superclass, then the method can be redefined or overridden. The process of redefining/overriding the method is called as Java – Method overriding.
The overridden method must have the same signature as that of the parent class method. Two methods are considered having same signature when their return type , method name, count and type of arguments are same.
Example For Java Method overriding:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Bird{ public void fly() { System.out.println("Bird is flying"); } } class Parrot extends Bird{ public void fly(){ System.out.println("Parrot is flying"); } public static void main( String args[]) { Parrot obj = new Parrot(); obj.fly(); } } |
Output:
Parrot is flying |
The overridden method must have the same signature as that of the parent class method.