Overloading and Overriding in Java: 10 basic difference

Overloading and Overriding in Java Scripting are some of the basic and important concepts that can get quite confusing at times. This module is an attempt to clarify the difference between the methods of Overloading and Overriding in Java Scripting.

What is Java Scripting?

Java Scripting is a dynamic computer programming language that allows one to implement certain complex features to a web page. It was initially created to “bring life” to web pages. This programming language was developed by James Gosling and since then has been one of the most widely used programming languages.

The concepts of Overloading and overriding are crucial to a better understanding of methods of Java Scripting.

What is Overloading in Java Scripting?

The method of Overloading in Java refers to the feature that permits a class to have more than one method under the same name on the condition that their argument list is different.

It is also known as constructor overloading in Java Scripting.

In other words, Overloading takes place when two and more methods occur in one class that consists of the same method name but different parameters.

There are three parameters of method overloading in Java Scripting, namely:

  1. Number of parameters
    • example:
      add(int, int)
      add(int, int, int)
  2. The data type of parameters
    • example:
      add(int, int)
      add(int, float)
  3. The sequence of data type
    • example:
      add(int, float)
      add(float, int)

Method overloading in Java Scripting is a perfect example of Static Polymorphism, also known as “compile-time binding” or “early binding”

Here, the binding of the method call to its definition takes place at “Compile-time”

Advantages of overloading in Java

  • It enables the programmer to create multiple methods of the same name having different parameters.
  • It gives programmers the flexibility to call the same or a similar method for a different type of data.
  • Methods in this method can have different return types.
  • It increases the readability of a program.
  • It can be implemented on constructors that allow different ways to initialize objects of a class.

Also Read: Differences Between Grid Computing and Cloud Computing

What is Overriding in Java Scripting?

The method of Overriding refers to the object-oriented programming feature in Java where a subclass (or child class) provides a different implementation for a method that is already implemented in its parent class.

Overriding is done so that the child class can give its own implementation to a method that has already been provided by the parent class.

In the method Overriding we must keep in mind that:

  • The argument list should be the same as the method overridden.
  • The return type declared should be the same as the subtype declared.
  • An instance method can be overridden only when they are inherited by a subclass.
  • If a method is declared static then it can’t be overridden.
  • Constructors can’t be overridden as well.

The method override in Java is a perfect example of runtime polymorphism.

It means that when a parent class reference points to the child class object, its call to override is determined at runtime.

it is thus also called “dynamic method dispatch”.

Advantages of method overriding

  • In this method, a subclass can give its own implementation-specific to itself without even modifying the parent class code.
  • A subclass can implement a parent class method based on requirements.
  • it can override the functionality of an existing method.

Example

class Animal{
   //Overridden method
   public void eat()
   {
      System.out.println("Animal is eating");
   }
}
class Panther extends Animal{
   //Overriding method
   public void eat(){
      System.out.println("Panther is eating");
   }
   public static void main( String args[]) {
      Panther obj = new Panther();
      //This will call the child class version of eat()
      obj.eat();
   }
}

Output:

“Panther is eating”

Overloading and Overriding in Java Scripting: Difference

The following are some of the basic difference between Overloading and Overriding in Java Scripting:

Based on the number of class in method:

  • Overloading takes place when two or more methods are in one class but have different parameters;
  • Whereas Overriding takes place when we have two methods under the same method name and parameters.

Based on polymorphism in the method:

  • Overloading is an example of a compile-time of polymorphism or static polymorphism
  • Whereas, Overriding is an example of runtime polymorphism or dynamic polymorphism.

Based on the same or different class in method:

  • Overloading is performed within the same class; whereas Overriding is performed with two or more classes.

Based on parameters in method:

  • Overloading consists of different parameters; Whereas, Overriding consists of different parameters.

Based on readability and specific implementation in method:

  • Overloading helps to increase the readability of the program; whereas, Overriding is used to grant the specific implementation of the method.

Based on inheritance in Method:

  • Overloading may or may not require inheritance: whereas, Overriding necessarily needs inheritance

Based on return type in method:

  • In Overloading the return type can be different under the condition that the parameters must be changed as well; Whereas, in overriding return type must always be the same.

Based on the static method:

  • In Overloading, the static method can be used; Whereas, in Overriding, there is no involvement of the static method.

Based on private and final methods:

  • The Overloading method in Java can overload private and final methods while the overriding method can’t.

Based on the argument list:

In the overriding method, the argument list is always different; Whereas, the argument list should always be the same in the overriding method.

We can, thus, conclude that the method Overloading in Java scripting is the method where there is one same class but different parameters and uses static or compile-time polymorphism whereas method Overriding in Java scripting involves multiple classes but the same parameters and uses runtime polymorphism.

Both, method overloading and method overriding are important in JavaScript as the former serves to increase the readability of the program and the latter helps to provide specific implementation already provided by its superclass.

Leave a Comment