1.Method Overriding in Java - Javatpoint
1.Method Overriding in Java - Javatpoint
ADVERTISEMENT
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 1 of 15
:
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
ADVERTISEMENT
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 2 of 15
:
Rules for Java Method Overriding
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 3 of 15
:
Understanding the problem without method overriding
Let's understand the problem that we may face in the program if we don't use
method overriding.
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 4 of 15
:
9. class Bike extends Vehicle{
10. public static void main(String args[]){
11. //creating an instance of child class
12. Bike obj = new Bike();
13. //calling the method with child class instance
14. obj.run();
15. }
16. }
ADVERTISEMENT
Test it Now
Output:
Vehicle is running
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 5 of 15
:
In this example, we have defined the run method in the subclass as defined in the
parent class but it has some specific implementation. The name and parameter of
the method are the same, and there is IS-A relationship between the classes, so
there is method overriding.
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 6 of 15
:
15. }
16. }
Test it Now
Output:
Consider a scenario where Bank is a class that provides functionality to get the rate
of interest. However, the rate of interest varies according to banks. For example,
SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest.
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 7 of 15
:
Java method overriding is mostly used in Runtime Polymorphism which we will
learn in next pages.
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 8 of 15
:
1. //Java Program to demonstrate the real scenario of Java Method Overriding
2. //where three classes are overriding the method of a parent class.
3. //Creating a parent class.
4. class Bank{
5. int getRateOfInterest(){return 0;}
6. }
7. //Creating child classes.
8. class SBI extends Bank{
9. int getRateOfInterest(){return 8;}
10. }
11.
12. class ICICI extends Bank{
13. int getRateOfInterest(){return 7;}
14. }
15. class AXIS extends Bank{
16. int getRateOfInterest(){return 9;}
17. }
18. //Test class to create objects and call the methods
19. class Test2{
20. public static void main(String args[]){
21. SBI s=new SBI();
22. ICICI i=new ICICI();
23. AXIS a=new AXIS();
24. System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
25. System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
26. System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 9 of 15
:
27. }
28. }
Test it Now
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 10 of 15
:
Can we override static method?
It is because the static method is bound with class whereas instance method is
bound with an object. Static belongs to the class area, and an instance belongs to
the heap area.
← Prev Next →
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 11 of 15
:
Youtube For Videos Join Our Youtube Channel: Join Now
Feedback
Regex tutorial
RxJS tutorial
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 12 of 15
:
RxJS tutorial
Keras tutorial
Preparation
Trending Technologies
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 13 of 15
:
Cloud Computing Hadoop tutorial ReactJS Tutorial
B.Tech / MCA
Operating System
Ethical Hacking
html tutorial
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 14 of 15
:
html tutorial
Automata Tutorial
https://www.javatpoint.com/method-overriding-in-java 18/06/24, 6 25 PM
Page 15 of 15
: