Method Overloading vs Overriding in Java


1. Objective

In this tutorial for Java, we are going to learn about the comparison between Method Overloading vs Overriding in Java with examples.

Method Overloading Vs Overriding in Java

Method Overloading Vs Overriding in Java

2. Introduction of Method Overloading and Overriding in Java

The basic difference between method Overloading vs method Overriding is that the former allows the different Java classes to have the same name with different parameters for input or numbers or both while the latter allows Java methods to have same parameters but having a different implementation.

Overloading supports compile-time polymorphism while Overriding supports runtime polymorphism in Java.

a. Example of Method Overloading in Java

class Sum
   {
       int add(int n1, int n2)
          {
             return n1+n2;
          }
       int add(int n1, int n2, int n3)
         {
            return n1+n2+n3;
         }
       int add(int n1, int n2, int n3, int n4)
        {
           return n1+n2+n3+n4;
        }
       int add(int n1, int n2, int n3, int n4, int n5)
       {
          return n1+n2+n3+n4+n5;
       } 
  public static void main(String args[])
          {
                Sum obj = new Sum();
                System.out.println("Sum of two numbers: "+obj.add(20, 21));
                System.out.println("Sum of three numbers: "+obj.add(20, 21, 22));
                System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23));
                System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24));
        }
  }

To learn about Method Overloading in Java, follow this link.

b. Example of Method Overriding in Java

package beginnersbook.com;

class CarClass
    {
        public int speedLimit()
           {
             return 100;
           } 
     }
class Ford extends CarClass
    {
       public int speedLimit()
          {
             return 150;
          }
        public static void main(String args[]
          {
              CarClass obj = new Ford();
              int num= obj.speedLimit();
              System.out.println("Speed Limit is: "+num);
         }
   }

To learn more about Method Overriding in Java, follow this link

3. Comparison between Java Method Overloading vs Overriding in Java

Below a brief comparison is discussed on method Overriding and overloading method in Java.

 OverloadingOverriding
MeaningIn this, the method can be used multiple times without rewriting it, we just have to change the parameters.In this, the method has the same name with same parameters as the parent class.
Behavior It adds or extends the behavior of the method.It changes the behavior of the method.
PolymorphismIt supports compile-time polymorphism.It supports runtime polymorphism.
Static methodIt supports overloading of a static method.It doesn’t support overriding of static methods.
BondStaticDynamic
InheritanceMay or may not require inheritanceIt always requires inheritance.
AssociationIt usually is associated with static programs.Associated with object-oriented programs.
SignatureDifferentSame
SpeedFasterSlower
ClassesIt doesn’t require more than one class for operation.A requirement of at least two classes.
LevelMethods can be provided with any access levels.Methods can have wide or same access level.

4. Conclusion

In this Java tutorial, we learned about the basic difference between the method overloading vs overriding method in Java, this helps us understand the two very important concepts of OOP. This given information is worth or not, say in a comment box.

See Also- Inheritance in Java & Java Regular Expression

For reference