Friday, September 21, 2012

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 Overridden method are 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 method can not be overridden but can be overloaded in Java.

7) Overloaded method are fast as compare to Overridden method in Java.

That's all on 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.

Other Java Fundamentals tutorials :
Difference between HashMap and ConcurrentHashMap in Java
Write Java program to print Fibonacci series upto 100
Write a Java program to find Armstrong number in Java
Difference between Path and Classpath in Java
What is Inheritance in Java and how to use it

1 comment:

Java67 Headline Animator