Overloading vs Overriding in Java
In the last couple of articles, we have seen What is method overloading and What is method overriding in Java and now we will see What is difference between overloading and overriding in Java. Overloading vs overriding is one of those frequently asked Java interview questions which can not be ignored. Overloading vs Overriding has appeared in almost every Java interview, mostly at beginner and intermediate level like 2 to 4 years experience. In fact, most of those tricky Java interview Questions came from Overloading and Overriding.
In the last couple of articles, we have seen What is method overloading and What is method overriding in Java and now we will see What is difference between overloading and overriding in Java. Overloading vs overriding is one of those frequently asked Java interview questions which can not be ignored. Overloading vs Overriding has appeared in almost every Java interview, mostly at beginner and intermediate level like 2 to 4 years experience. In fact, most of those tricky Java interview Questions came from Overloading and Overriding.
It's one of the tricky fundamentals to understand. In this article, we will some important difference between Overloading and Overriding which not only help to understand the concept better but also serves as a good recap for Java interviews.
1. Timing
Difference between Overloading and Overriding in Java
Here are some of the most common differences between both of them. If you are working in Java for more than 1 year, you might be familiar with all of them but anyway its a good revision :1. Timing
The first and major difference between Overloading and Overriding is that the former occurs during compile time while the latter occurs during runtime.
2.Location
The second difference between Overloading and Overriding is that you can overload a method in the same class but you can only override a method in a subclass.
3. Static modifier
3. Static modifier
The third difference is that you can overload the static method in Java but you can not override the static method in Java. In fact, when you declare the same method in Sub Class it's known as method hiding because it hides the superclass method instead of overriding it.
4. Resolution
Overloaded methods are bonded using static binding and the type of reference variable is used, while the Overridden method is bonded using dynamic bonding based upon the actual Objects.
5. Rules
Rules of Overloading and Overriding are different in Java. In order to overload a method you need to change its method signature but that is not required for overriding any method in Java.
6. private and final methods
6. private and final methods
Another difference between method overloading and overriding is that private and final methods can not be overridden but can be overloaded in Java.
7. Speed
7. Speed
The overloaded method is fast as compare to the Overridden method in Java.
And here is a diagram worth 1000 words to explain the overloading vs overriding concept in Java:
That's all on the Difference between method overloading and overriding in Java. Apart from rules of overloading and overriding, these are some important differences that are worth remembering while overloading or overriding any method in Java.
Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
thnks a lot
ReplyDeleteI think point 2 is incorrect because we can overload a method in sub class also http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9
ReplyDelete