Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      Method Overloading vs Method Overriding in Java

      Method Overloading vs Method Overriding
      Blog

      Method Overloading vs Method Overriding in Java

      What is the difference between Method Overloading And Overriding?

      Introduction

      When it comes to writing programs with an emphasis on objects, Java is without a peer. Working with Java is made much simpler by using notions such as classes, objects, inheritance, polymorphism, and other similar ideas. The code is both efficient and less complicated because of the fact that it is easy to access and has an easy syntax.

      Method Overloading in Java

      Because of method overloading, two distinct methods can share the same name but behave differently according to the arguments they receive or the types of arguments they receive. It is possible that this is connected to compile-time polymorphism. When we are overloading methods in Java, there are a few things that we need to bear in mind, and those things are listed below.

      The ability to avoid repeatedly defining the same function as a result of employing method overloading in Java is the primary benefit of this programming technique. Without it, we would have to do so each time we wanted to accomplish the same thing. Given that the two ways in the following illustration are, in essence, doing division, it is possible for us to have distinct methods with the same name but distinct parameters. Additionally, it helps with polymorphism at compile time.

      • It is not possible for us to overload a return type.
      • Even if we have the ability to overload static methods, the arguments and input parameters must be distinct each time.
      • It is impossible for us to overload two methods if the only thing that differentiates them is a static keyword.
      • The main() method, just like any other static method, is capable of being overloaded.

      Illustration of Method Overloading

      public class SLA

      {
      public static void main(String[] args){
      System.out.println(“Hi”);
      SLA.main(“Students”);
      }
      public static void main(String arg1){
      System.out.println(“Learn” + arg1);
      SLA.main(“Learn” , “to succeed”);
      }
      public static void main(String arg1 , String arg2){
      System.out.println(“Hi” , +arg1 , +arg2);
      }
      }

      Output :

      Hi, Learn Students
      Hi, Learn to succeed

      Program for overloading static methods in Java.

      public class Test{
      public static int func(int x ){
      return ;1000
      }
      public static char func(int x , int y){
      return “SLA”;
      }
      public static void main(String args[]){
      System.out.println(func(1));
      System.out.println(func(1,3));
      }
      }

      Output :

      1000
      SLA

      Program for overloading three methods with a similar name.

      public class Add{
      public int add(int x , int y){
      return (x + y);
      }
      public int add(int x , int y , int z){
      return (x + y + z) ;
      }
      public double add(double x , double y){
      return (x + y);
      }
      public static void main( String args[]){
      Add ob = new Add();
      ob.add(11,22);
      ob.add(12,22,33);
      ob.add(11.1 , 22.2);
      }
      }

      Output :

      33
      66
      33.3

      Method Overriding in Java : What Is It?

      Java’s inheritance system is based on a connection between parent and child classes. When two classes share a method’s name and its arguments or parameters, one of those methods will always be used instead. The object’s state determines which method is called.

      The parent class method will be superseded if the method is called from within a child class object. Otherwise, the method of the parent class will be invoked if the calling object is a member of that class. The use of overridden methods is also useful for achieving polymorphism in Java at runtime.

      Sets Of Guidelines To Implement Method Overriding

      • There is no other way to increase the overridden method’s access but to use an access modifier.
      • Overriding is not possible in a final method.
      • You can’t change or modify a static method.
      • You can’t change or disable private methods.
      • The overridden method  need to possess the similar return type.
      • Using the super keyword, we may invoke the original method from the parent class in the overridden one.
      • It is impossible to override a constructor if both the parent class and the child class use the same name for their respective constructors.

      Illustration of method overriding in Java

      class Parent{

      void view(){

      System.out.println(“this is a parent class method);

      }}

      class Child extends Parent{

      void view(){

      System.out.println(“this is a child class method);

      }}

      public static void main(String args[]){

      Parent ob = new Parent();

      ob.view();

      Parent ob1 = new Child();

      ob1.view();

      Output :

      This is a child class method

      Illustration

      class Parent {

      void show(){

      System.out.println(“parent class method”);

      }

      class Child extends Parent {

      void show(){

      super.show();

      System.out.println(“child class method”);

      }

      public static void main(String args[]){

      Parent ob = new Child();

      ob.show();

      }

      }

      Output:

      parent class method

      child class method

      Method Overloading Vs Method Overriding
      S.No Characteristics Overloading Method Overriding Method
      1 Definition Method overloading in Java refers to the situation where a class contains multiple methods with the same name but distinct in parameters. In Java, method overriding occurs when a subclass customizes the execution of a method inherited from its superclass.
      2 Argument type Overloading requires a change in argument type (or at least order) The parameter type must be the same when overriding (including order).
      3 Method Signature The signature of the overloading method ought to be unique. The signature of the method being overridden must remain the same.
      4 Return type In method overloading, a different return type is allowed, or the same type as the original method. In Java 1.4 and earlier versions, the return type must be the same when overriding a method; in Java 1.5 and later, however, the return type can be changed while maintaining covariance.
      5 Class Overloading method is often done inside the same class. Inheritance is used to perform method overriding in two classes (Is-A relationship).
      6 Final/static/Private Method Java allows for the overloading of private, static, and final methods. Private, static, and final methods do not qualify for the overriding concept. Java allows overriding of private, static, and final methods.
      7 Access Modifiers It’s possible to use any kind of access modifier you choose while overloading. When overriding, the access modifier of the subclass method must be equal to or greater than the access modifier of the superclass method; in other words, we cannot decrease the visibility of the subclass method.
      8 Throws clause The idea of overloading allows for any kind of exception to be thrown. a. If a child class’s method throws a checked exception, the parent class’s overridden method must also throw the same checked exception as its parent, or a compile-time error will result.
      9 Method Resolution The compiler automatically handles method resolution in method overloading based on the reference type. The Java Virtual Machine (JVM) always handles the method resolution in overriding depending on the runtime object.
      10 Polymorphism Method overloading is shall also be addressed as compile-time polymorphism, or early binding or static polymorphism. Method overriding, or runtime polymorphism, dynamic polymorphism, or late binding, are several names for the same concept.

      The Java programming language is a state-of-the-art implementation of object-oriented programming that has many useful features. Because of Java’s prevalence and importance, anyone hoping to become a developer should be well-versed in the language’s basics. Having a firm grasp on the meaning of method overloading and overriding in Java is crucial due to the interplay of classes, objects, and ideas like inheritance and polymorphism. Learn Java the right way by signing up for SLA’s Java Training in Chennai.

      1
      Softlogic-Academy