Differentiate between method overloading and overriding. How polymorphism achieved in JAVA?


Q.) Differentiate between method overloading and overriding. How polymorphism achieved in JAVA?

Subject: Object Oriented Programming and Methodology

Method Overloading:

  • Method overloading allows a class to have multiple methods with the same name but different parameters.
  • The methods must differ in the number, type, or order of their parameters.
  • The return type does not affect overloading.
  • Method overloading is achieved at compile time.
  • It is a static binding.

Method Overriding:

  • Method overriding occurs when a subclass provides a new implementation of a method that is already defined in its superclass.
  • The method signature (name and parameters) must be exactly the same in both the subclass and superclass.
  • The return type of the overriding method must be the same or a subclass of the return type of the overridden method.
  • Method overriding is achieved at runtime.
  • It is a dynamic binding.

Polymorphism in JAVA:

Polymorphism is the ability of an object to take different forms. In JAVA, polymorphism is achieved through method overriding and method overloading.

  • Method Overriding:

When a subclass overrides a method of its superclass, the object of the subclass can call either the overridden method or the superclass method. The method that is called depends on the type of the object. This is known as dynamic binding or late binding.

  • Method Overloading:

When a class has multiple methods with the same name but different parameters, the method that is called depends on the arguments that are passed to the method. This is known as static binding or early binding.

Polymorphism allows us to write code that is more flexible and maintainable. It also makes it easier to create new subclasses without having to rewrite large amounts of code.