Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method.
A method can only be written in Subclass not in same class. The argument list should be exactly the same as that of the overridden method. The return type should be the same or a sub type of the return type declared in the original overridden method in the super class. A method declared final cannot be overridden.
Example:
- class Human
- { publicvoid eat()
- { System.out.println("Human is eating"); }}
- class Boy extends Human
- { publicvoid eat()
- { System.out.println("Boy is eating"); }
- publicstaticvoid main( String args[])
- { Boy obj = new Boy(); obj.eat(); }}
Call us @ 9825618292
Visit us @ http://tccicomputercoaching.com
No comments:
Post a Comment