Method Overloading v/s Method Overriding

The capability of more than one objects to respond to the same message in a different way can be broadly categorized into two types: Method Overloading and Method Overriding.

METHOD OVERLOADING

  • If we have methods with the same name but having different signatures or behaviors in the same scope than the methods are known as overloaded methods and the process is known as method overloading
  • In simple language, we can understand it as a Defining function with the same name, different arguments in same class or base and derive class

Note

  • A Method can be overloaded in the same class as well as in its sub-class
  • Constructors can also be overloaded. In this case, we call the process as constructor overloading

Example

class Example{
public static void main(String[] args){
  A obj=new A(); //default constructor called of Object class which is the super class of all the classes in Java
  obj.display();
  obj.display("codedbug");
  obj.display(10);
  obj.display(20.2);
/*
Remember in case if we will have no display method with int type signature in the class A than the display method with signature double will be called. This is processed at run time by the java run time environment
*/
  }
}

class A{
  void display(){
	System.out.println("I am in display of A");
  }
  void display(String name){
	System.out.println("Welcome to "+name);
  }
   void display(int number){
	System.out.println("The Received number (int) is "+number);
   }
   void display(double number){
	System.out.println("The received number (double) is "+number);
   }
}

Output

I am in display of A
Welcome to codedbug
The Received number (int) is 10
The received number (double) is 20.2

Note: A method having different return types but not having different signatures cannot be said as overloaded methods because it will result in an error stating “signature should be different”.

Example

void display(){
}
and
int display(){
}

Example of overloaded methods in System class is print() and println()
Now studying about method overloading in Java raises a question in our minds.
Do you know what is that question?

Question: Can We overload the main method of Java?

Answer: The simple answer is Yes. This is how we will prove it:

class Main{
 static void main(){
 System.out.println("Overloading Achieved!");
 }
 public static void main(String[] args){
 System.out.println("I am the original Main Method.");
 main();
 }
}

Output

I am the original Main Method.
Overloading Achieved!

Some important rules to remember

  • You can change the argument list of an overloaded method
  • One can change the return type(but at least one argument should be changed ) and access modifier of the overloaded method
  • You can declare the new checked exceptions in the overloaded method
  • You can overload a method in the same or in the subclass

METHOD OVERRIDING

  • Method overriding means having a different implementation of the same method in the inherited or derived class.
  • These two methods would have the same signature, but different implementation.
    One of these would exist in the base or the superclass and other in the derived class.These methods cannot exist in the same class.
  • In simple language, we can understand it as Same Name, Same Arguments, Base and Derive Class

Example

class A{
 int num1;
 A(int num1){
     this.num1=num1;
 }
 int show(){
   return num1;
 }
}
class B extends A{
 int num2;
 B(int num1, int num2){
     super(num1);
     this.num2=num2;
 }
 @Override
 int show(){
  return super.show()+num2;
 }
}
class Example{
  public static void main(String[] args){
  A obj=new B(10, 20);
  System.out.println(obj.show());
  }
}

Output

30

Note

A obj=new B();// here A is the super class so it can hold the ref. of the derived class the reverse will generate an error.

We use @overrride annotation just before the method to denote an overridden method.

Some important rules to remember:

  • You cannot override a final method
  • You cannot override a static method to make it non-static
  •  The overriding method and the overridden method must have the same return type. Java 6.0 allows covariant return types(instead of base class)
  • The number and types of parameters in the overriding and overridden should be same, however, the name can be different
  • If the overriding method has a throws clause in its declaration, then the overridden method can also have throws clause(elimination is possible, derived classes can be used)
  • You cannot override a method that is not inherited
  • You cannot override a method to make it is less accessible, but vice versa is true

Comparing Overloading with Overriding

POINT OF DIFFERENCE OVERLOADING OVERRIDING
Arguments Change number/type/sequence cannot change number/type/sequence
Access Modifiers Change Same/Increase Access
Exceptions Change You can reduce or eliminate. No new Exceptions can be added except Unchecked Exceptions
Return Type Change Cannot Change