×
☰ See All Chapters

Method Overloading vs Method Overriding in Java

 

Method Overloading

Method  Overriding

Definition

In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.

In Method Overriding, sub class has the same method with same name and exactly the same number and type of parameters and same return type as a super class.

Meaning

Method Overloading means more than one method shares the same name in the class but having different signature.

Method Overriding means method of base class is re-defined in the derived class having same signature.

Behavior

Method Overloading is to “add” or “extend” more to method’s behavior

Method Overriding is to “Change” existing behavior of method.

Polymorphism

It is a compile time polymorphism.

It is a run time polymorphism.

Inheritance

It may or may not need inheritance in Method Overloading.

It always requires inheritance in Method Overriding.

Signature

In Method Overloading, methods must have different signature.

In Method Overriding, methods must have same signature.

Relationship of Methods

In Method Overloading, relationship is there between methods of same class.

In Method Overriding, relationship is there between methods of super class and sub class.

Criteria

In Method Overloading, methods have same name different signatures but in the same class. Criteria is number, order, data type of parameters

In Method Overriding, methods have same name and same signature but in the different class. Criteria is type of object.

No. of Classes

Method Overloading does not require more than one class for overloading.

Method Overriding requires at least two classes for overriding.

Java Method Overloading example

package com.java4coding;

 

public class HelloWorld {

        String join(String a, String b) {

                return a + b;

        }

 

        String add(String a, String b, String c) {

                return a + b + c;

        }

}

Java Method Overriding example

class Fruit {

        void color() {

                System.out.println("Color is...");

        }

}

 

class Banana extends Fruit {

        void color() {

                System.out.println("Color is Yellow");

        }

}

 


All Chapters
Author