Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday, 21 July 2017

Difference Between Method Overloading and Overriding In Java

Difference Between Method Overloading and Method Overriding

Difference Between Overloading and Overriding in java

Now here we will learn what is the main differences between method overloading and method overriding in java. This is very important question for java interviews. So let's starts

First start with method overloading in java.

Method Overloading

  • Whenever we have more than one method with the same name but different number of arguments or different data types in the same class is called method overloading in java.
  • Method overloading are happens at compile-time.
  • Method overloading is also known as compile-time polymorphism or static polymorphism or early binding.


  • Method overloading increase the readability of a program.
  • Method overloading are performed in the same class, there is no need of inheritance i.e parent child relationship or Is-a relationship.
  • In java private, static, and final methods can be overloaded.
  • Method overloading cannot performed by changing the return type of the method only. In method overloading, return type can be same or different but parameter must be different.
  • Method overloading give better performance.

Java Method Overloading Example

In the below example, we have taken two method with the same name called marks() but different parameters.

public class Student
{
void marks(int a)//single parameter
{
System.out.println(a);
}
void marks(int a, int b)//double parameter
{
System.out.println(a+b);
}
public static void main(String args[])
{
Student s = new Student();
s.marks(50);
s.marks(50, 30);
}
}

output : 50
              80


Java Method Overloading Example 2

public class Addition
{
int add(int a)//with one parameter
{
return a;
}
int add(int a, int b)//with two parameter
{
return a+b;
}
int add(int a, int b, int c)//with three parameter
{
return a+b+c;
}
public static void main(String args[])
{
Addition a = new Addition();
System.out.println(a.add(10));
System.out.println(a.add(10, 20));
System.out.println(a.add(10, 20, 30));
}
}

output : 10
              30
              60

In the above example, there are three methods with same name called add() but the different number of arguments.


Method Overriding

  • Whenever we have parent class method and child class method are same name and same number of arguments or parameters is called method overriding in java.
  • Method overriding are happens at run-time.
  • Method overriding is also known as runtime polymorphism or dynamic polymorphism or late binding.
  • Method overriding is used to provide specific implementation of a method which is already present in parent class or super class.
  • Without inheritance we cannot achieve method overriding in java. There must be parent-child relationship or Is-a relationship.
  • In java private, static, final method cannot be override.
  • Return type must be same or covariant in method overriding.
  • Performance is slow.



Java Method Overriding Example

class Parent
{
void run()
{
System.out.println("Parent runs in the morning");
}
}
class Child extends Parent
{
void run()
{
System.out.println("Child runs in the evening");
}
public static void main(String args[])
{
Child c = new Child();
c.run();
}
}

output : Child runs in the evening

Read More:

Difference Between Constructor and Method in Java.
Difference Between HashSet and HashMap in Java
Difference Between String and StringBuffer in Java.
Difference Between C++ and Java.
Method Overloading Interview Questions in Java.

In the above example, there is a Parent and Child class both have same name method i.e run() method.

Share:

3 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate