Pages

concept of method overriding

Having the same method in the subclass as declared in the parent class is known as method overriding. If a subclass provides a specific implementation of a method that is already provided by its super class, it is known as Method Overriding.

Advantages :

1) Method Overriding is used to provide specific implementation of a method that is already provided by its super class.

2) Method Overriding is used for Runtime Polymorphism.

Rules :

1) method must have same name as in the parent class.

2) method must have same parameter as in the parent class.


class Animal{ void eat(){System.out.println("eating");} } class Human extends Animal{ void eat(){System.out.println("eating delicious food");} public static void main(String args[]){ Human h= new Human(); h.eat(); }
}

Output eating delicious food

 
JAVA TUTORIALS HOMEPAGE


No comments:

Post a Comment