Uncategorized

Method Overloading and Method Overriding in java.

Method Overloading and Method Overriding in java.

Difference between Method Overloading and Method Overriding.

NO. Method Overloading Method Overriding
1) When a class having several methods with the same name but with the different arguments, then it is known as method overloading. When a class having two methods with the same arguments, but different implementations and if a parent class (super class) method is redefined in its child class (sub class) then, it is known as method overriding.
2) Method overloading is performed within one class. Method overriding occurs in two classes or more classes.
3) It is the example of compile time polymorphism. It is the example of run time polymorphism.
4) In method overloading access modifiers can be changed. In method overriding access modifiers can be changed if and only if child class must not have more restrictive access modifiers.
5) Method overloading may have same or different return type. Method overriding must have the same return type.
6) In method overloading argument list can be differ. In method overriding argument list must be same.
7) Constructors can be overloaded. Constructors can’t be overridden.
8) Private and final method can be overloaded. Private and final method can’t be overridden.
9) Method overloading gives better performance compared to method overriding. Method overriding does not give better performance compared to method overloading.

Method Overloading:

Definition: if a class having two or more methods with same name but with the different parameters or arguments, then it is called as Method Overloading.

In java method overloading can be done in three ways.

  1. By changing the data type of arguments.
  2. By changing the number of arguments.
  3. By changing the return-type of a method.

Now let’s see the java program of Method Overloading by changing the data type of arguments.

Addition.java

public class Addition
{
public void sum(int a,int b)
{
System.out.println("sum of "+a+" and "+b+" is: "+(a+b));
}
public void sum(float a,float b)
{
System.out.println("sum of "+a+" and "+b+" is: "+(a+b));
}
public void sum(double a,double b)
{
System.out.println("sum of "+a+" and "+b+" is: "+(a+b));
}
public static void main(String[] args)
{
Addition obj=new Addition();
obj.sum(10,20);
obj.sum(10.5f,20.5f);
obj.sum(3.14,3.14);
}
}

Output:

sum of 10 and 20 is: 30
sum of 10.5 and 20.5 is: 31.0
sum of 3.14 and 3.14 is: 6.28

Now let’s see the java program of Method Overloading by changing the number of arguments.

ChangeArgumentTest.java

public class ChangeArgumentTest
{
public void changeArg(int x)
{
System.out.println("Cube of "+x+" is: "+(x*x*x));
}
public void changeArg(int x,int y)
{
System.out.println("Sum of "+x+" and "+y+" is: "+(x+y));
}
public void changeArg(int x,int y,int z)
{
System.out.println("Sum of "+x+", "+y+" and "+z+" is: "+(x+y+z));
}
public static void main(String[] args)
{
ChangeArgumentTest obj=new ChangeArgumentTest();
obj.changeArg(5);
obj.changeArg(10, 20);
obj.changeArg(10, 20, 30);
}
}

Output:

Cube of 5 is: 125
Sum of 10 and 20 is: 30
Sum of 10, 20 and 30 is: 60

Now let’s see the java program of Method Overloading by changing the return-type of method.

ChangeReturnTypeTest.java

public class ChangeReturnTypeTest
{
public void add(int x, int y)
{
System.out.println("Sum of "+x+" and "+y+" is: "+(x+y));
}
public int add(int x,int y,int z)
{
System.out.println("Sum of "+x+" ,"+y+" and "+z+" is: "+(x+y+z));
return x+y+z;
}
public float add(float x,float y)
{
System.out.println("Sum of "+x+" and "+y+" is: "+(x+y));
return x+y;
}
public static void main(String[] args)
{
ChangeReturnTypeTest obj=new ChangeReturnTypeTest();
obj.add(5,10);
obj.add(10, 20, 30);
obj.add(10.25f, 20.25f);
}
}

Output:

Sum of 5 and 10 is: 15
Sum of 10, 20 and 30 is: 60
Sum of 10.25 and 20.25 is: 30.5

Method Overriding:

Definition: If a method of super class (parent class) is redefined in its sub class (child class), then it is called as Method Overriding.

Child.java

class Parent
{
public void sayHello()
{
System.out.println("Hello this is Parent class.");
}
}
class Child extends Parent
{
@Override
public void sayHello()
{
System.out.println("Hello this is Child class.");
}
public static void main(String[] args)
{
Parent p=new Parent();//Caling sayHello() from Parent class.
p.sayHello();
Child c=new Child();//Caling sayHello() from Child class.
c.sayHello();
Parent p1=new Child();//This process is known as Upcasting.
p1.sayHello();
}
}

Output: 

Hello this is Parent class.
Hello this is Child class.
Hello this is Child class.

Note:  A Parent class is also known as Super class or Base class or vice versa. And a Child class is also known as Sub class or Derived class or vice versa.

Leave a Reply

Your email address will not be published. Required fields are marked *