What is method overriding in Java? - tccicomputercoaching.com

Page 1

What is method overriding in Java? tccicomputercoaching.com When method of child class redefines the method of parent class then it is called method overriding in Java.

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 overridn 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: 1. 2. 3. 4. 5. 6. 7. 8.

class Human { public void eat() { System.out.println("Human is eating"); }} class Boy extends Human { public void eat() { System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); obj.eat(); }} To learn more in detail about Java at TCCI. Call us @ 9825618292


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.