ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Overriding

Overriding

If the child class has the same method as the parent class we call it method overriding in java.
Example:

  1. class House
  2. {
  3. void display()
  4. {
  5. System.out.println(“House”);
  6. }
  7. }
  8.  
  9. class Room extends House
  10. {
  11. void display()
  12. {
  13. System.out.println(“Room”);
  14. }
  15. }
  16.  
  17. class Basic
  18. {
  19.  
  20. public static void main(String args[])
  21. {
  22. House h=new House();
  23. House o=new Room();
  24. h.display();
  25. o.display();
  26. }
  27.  
  28. }

Output:
House
Room

Rules for Java Method Overriding
1.method must have same name as in the parent class.
2.method must have same parameter as in the parent class.
3.must be inheritance.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!