Java programming language is the best choice when it comes to object-oriented programming. With concepts like classes, objects, Inheritance, Polymorphism, etc, it becomes extremely easy to work with Java. Ease of access and easy syntax makes the code efficient and less complex as well. In this article, we will learn about method overloading and overriding in Java. Following are the topics discussed in this blog:
- What is Method Overloading in Java?
- Why Method Overloading?
- Method Overloading Examples
- What is Method Overriding?
- Rules For Method Overriding
- Method Overriding Example
- Overloading vs Overriding: Differences between Method Overload and Method overriding
What is Method Overloading in Java?
Method overloading allows the method to have the same name which differs on the basis of arguments or the argument types. It can be related to compile-time polymorphism. Following are a few pointers that we have to keep in mind while overloading methods in Java.
We cannot overload a return type.
Although we can overload static methods, the arguments or input parameters have to be different.
We cannot overload two methods if they only differ by a static keyword.
Like other static methods, the main() method can also be overloaded.
Let’s take a look at a simple program to understand how method overloading works in python.
public class Div{ public int div(int a , int b){ return (a / b); } public int div(int a , int b , int c){ return ((a + b ) / c); } public static void main(String args[]){ Div ob = new Div(); ob.div(10 , 2); ob.div(10, 2 , 3); } }
Output: 5 4
In the above program, we have two methods with the same name but different parameters. This is how the method overloading works in Java.
Why Method Overloading?
The main advantage of using method overloading in Java is that it gives us the liberty to not define a function again and again for doing the same thing. In the below example, the two methods are basically performing division, so we can have different methods with the same name but with different parameters. It also helps in compile-time polymorphism.
Overloading the main() method:
Following is an example to overload the main() method in java.
public class Edureka{ public static void main(String[] args){ System.out.println("hello"); Edureka.main("edurekan"); } public static void main(String arg1){ System.out.println(" welcome" + arg1); Edureka.main("welcome" , "to edureka"); } public static void main(String arg1 , String arg2){ System.out.println("hello" , +arg1 , +arg2); } }
Output: hello welcome edurekan hello, welcome to edureka
Method Overloading Examples
- Program to overload static methods in java.
public class Test{ public static int func(int a ){ return 100; } public static char func(int a , int b){ return "edureka"; } public static void main(String args[]){ System.out.println(func(1)); System.out.println(func(1,3)); } }
Output: 100 edureka
- Program to overload three methods with the same name.
public class Add{ public int add(int a , int b){ return (a + b); } public int add(int a , int b , int c){ return (a + b + c) ; } public double add(double a , double b){ return (a + b); } public static void main( String args[]){ Add ob = new Add(); ob.add(15,25); ob.add(15,25,35); ob.add(11.5 , 22.5); } }
Output: 40 75 34
What Is Method Overriding in Java?
Inheritance in java involves a relationship between parent and child classes. Whenever both the classes contain methods with the same name and arguments or parameters it is certain that one of the methods will override the other method during execution. The method that will be executed depends on the object.
If the child class object calls the method, the child class method will override the parent class method. Otherwise, if the parent class object calls the method, the parent class method will be executed.
Method overriding also helps in implementing runtime polymorphism in java. Let’s take a simple example to understand how method overriding works in java.
class Parent{ void view(){ System.out.println("this is a parent class method); }} class Child extends Parent{ void view(){ System.out.println("this is a child class method); }} public static void main(String args[]){ Parent ob = new Parent(); ob.view(); Parent ob1 = new Child(); ob1.view();
Output: this is a child class method
Rules For Method Overriding
The access modifier can only allow more access for the overridden method.
A final method does not support method overriding.
A static method cannot be overridden.
Private methods cannot be overridden.
The return type of the overriding method must be the same.
We can call the parent class method in the overriding method using the super keyword.
A constructor cannot be overridden because a child class and a parent class cannot have the constructor with the same name.
Method Overriding Example
- Program to show overriding using super keyword
class Parent { void show(){ System.out.println("parent class method"); } class Child extends Parent { void show(){ super.show(); System.out.println("child class method"); } public static void main(String args[]){ Parent ob = new Child(); ob.show(); } }
Output: parent class method child class method
Overloading vs Overriding: Difference between Method Overloading and Method Overriding
Following are the key differences between method overloading and overriding in Java.
Method Overloading | Method Overriding |
|
|
|
|
|
|
|
|
|
|
|
|
In this blog, we have discussed method overloading and method overriding in detail. With the involvement of classes, objects, and concepts like inheritance and polymorphism it becomes fairly important to get a clear idea of what it means to overload or override methods in java.
If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.
Java programming language is a pinnacle in object-oriented programming and incorporates a lot of applications. With the demand and popularity, an aspiring java developer must be proficient in the fundamental concepts of the programming language. Enroll to Edureka’s Java Certification program to kick-start your learning.
Got a question for us? please mention this in the comments section of this article on “method overloading vs method overriding in java” and we will get back to you as soon as possible.