Overriding

0
12

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
(Visited 15 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here