Friday 20 March 2020

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 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:

  1. class Human
  2. { publicvoid eat()
  3. { System.out.println("Human is eating"); }}
  4. class Boy extends Human
  5. { publicvoid eat()
  6. { System.out.println("Boy is eating"); }
  7. publicstaticvoid main( String args[])
  8. { Boy obj = new Boy(); obj.eat(); }}
To learn more in detail about Java at TCCI.

Call us @ 9825618292

Visit us @ http://tccicomputercoaching.com

No comments:

Post a Comment