What is difference between Overloading and Overriding in Java

Overloading vs Overriding in Java
In 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 question which can not be ignored. Overloading vs Overriding has appeared in almost every Java interview, mostly at beginner and intermediate level e.g. 2 to 4 years experience. In fact most of those tricky Java interview Question came from Overloading and Overriding. It's one of the tricky fundamental to understand. In this article we will some important difference between Overloading and Overriding which not only help to understand concept better but also serves as good recap for Java interviews.


Difference between Overloading and Overriding in Java

Difference between Overloading and Overriding in Java methodsHere 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 any way its good revision :


1) First and major difference between Overloading and Overriding is that former occur during compile time while later occur during runtime.

2) Second difference between Overloading and Overriding is that, you can overload method in same class but you can only override method in sub class.



3) Third difference is that you can overload static method in Java but you can not override static method in Java. In fact when you declare same method in Sub Class it's known as method hiding because it hide super class method instead of overriding it.

4) Overloaded methods are bonded using static binding and Type of reference variable is used, while the Overridden method is bonded using dynamic bonding based upon actual Object.

5) Rules of Overloading and Overriding is 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) Another difference between method overloading and overriding is that private and final methods can not be overridden but can be overloaded in Java.

7) The overloaded method are fast as compare to the Overridden method 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 which is worth remembering while overloading or overriding any method in Java.

Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]


Other Java Fundamentals tutorials :



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.

P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

2 comments:

  1. I 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