Difference Between Method Overloading And Method overriding In java
In this particular tutorial we are going to discuss one of the most important topic of core java-difference between method overloading and method overriding.Though both of these(method overloading and method overriding) are used to achieve Polymorphism in java but these are two quite different phenomenon.
First we will discuss the properties of both individually and then we can easily find out the major and minor differences between these two.
The definition of method overloading above sounds pretty simple,but it becomes complicated if we talk about the term "different parameters", how we will identify when the parameters of two methods are different?
To answer the above problem there are two rules,and any one or both of these these rules should be satisfied for method overloading.Here are the rules-
Example of method overloading using different number of parameters-
Program-
What if we only change the return type or name of parameters as explained not to do above-
Method overloading is compile time phenomenon and its the duty of compiler to determine whether a method is overloaded correctly,so if we do any of the things recommended above not to do then we will get a compile time error.
Now as we are completely aware about method overloading,so now we will cover our next topic "method overriding".
Method overriding is a run-time phenomenon not a compile time phenomenon like method overloading.
Example of method overriding-
Program-
So these were some of the basic differences between method overloading and method overriding,i hope you understood both the topics easily and perfectly.This is a very important question which is asked in interviews, specially at fresher level so be prepared.
In our next articles we will discuss some other important java topics so stay in touch.
First we will discuss the properties of both individually and then we can easily find out the major and minor differences between these two.
What is method overloading-
Definition-"When two or more then two methods in the same class have the same name but different parameters,then this phenomenon is known as method overloading".The definition of method overloading above sounds pretty simple,but it becomes complicated if we talk about the term "different parameters", how we will identify when the parameters of two methods are different?
To answer the above problem there are two rules,and any one or both of these these rules should be satisfied for method overloading.Here are the rules-
Conditions for method overloading in java
- For method overloading in java the number of parameters in methods should be different.
- Type of the parameters should be different be different
OR
Example of method overloading using different number of parameters-
Program-
package in.blogger.tutorialsinn; class MethodOverloading { // first method static int addNumbers(int a, int b) { System.out.println("sum of the numbers is " + (a + b)); return (a + b); } // second method static int addNumbers(int a, int b, int c) { System.out.println("sum of the numbers is " + (a + b + c)); return (a + b + c); } } public class Testing { public static void main(String[] args) { // it will call first method MethodOverloading.addNumbers(1, 2); // it will call second method MethodOverloading.addNumbers(1, 2, 3); } }
How to not overload methods-
- If return type is the only thing that we have changed of two methods with same name in the same class then it is not a method overloading.
- If we only change the name of the parameters in the methods with same name in the same class, then it is not a method overloading
What if we only change the return type or name of parameters as explained not to do above-
Method overloading is compile time phenomenon and its the duty of compiler to determine whether a method is overloaded correctly,so if we do any of the things recommended above not to do then we will get a compile time error.
Now as we are completely aware about method overloading,so now we will cover our next topic "method overriding".
Method Overriding In java-
Definition-"If we redefine a method in the subclass with the same name,same return type,same number of parameters and same type of the parameters as it was in the super class then this phenomenon is called method overriding".Method overriding is a run-time phenomenon not a compile time phenomenon like method overloading.
Example of method overriding-
Program-
package in.blogger.tutorialsinn; //super class class Animal{ String sound(){ return "some sound"; } } //sub class class Dog extends Animal{ String sound(){ return "barking"; } }Now as we have discussed both method overloading and method overriding , so now is the time to point out some differences between the two-
Method Overloading | Method Overriding |
---|---|
Method overloading is a compile-time phenomenon | Method overriding is a run-time phenomenon |
All the overloaded methods are in the same class. | One method is in super class and another is in sub class. |
Parameters must be different in method overloading. | Parameters must be same in method overriding. |
In method overloading return type could be same or different. | In method overriding return-type should be same |
So these were some of the basic differences between method overloading and method overriding,i hope you understood both the topics easily and perfectly.This is a very important question which is asked in interviews, specially at fresher level so be prepared.
In our next articles we will discuss some other important java topics so stay in touch.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment