Tutorial for Polymorphism in OOPS

What is Polymorphism?

Polymorphism, as its name implies, comes in a variety of forms. OOP allows for the use of a variety of techniques I. Simply expressed, they have to do with how methods are used, and how different methods operate for different objects. Method Overriding and Method Overloading are two ways that polymorphic methods allow for different actions to be done.

The benefits of Polymorphism are:

  • Objects of different types can be passed through the same interface
  • Method overriding
  • Method overloading

Method Overriding:- Overriding is used in the polymorphism of runtime. By overriding a parent class’s method, a child class can implement a different method. In the case of our dogs, we may prefer a specific type of bark that is distinct from the generic type of dog (TrackingDog).


TrackingDog’s overriding the bark() method

Method Overloading:- Overloading is used at the polymorphic build time. Although methods and functions may have the same name, the call method has its own set of arguments. Different results may be obtained depending on the number of parameters entered.

Example:- There are no parameters supplied to the updateAttendance() method. The count will be added one day. When the updateAttendance(4) argument is used, the x xupdateAttendance(x) parameter is used, and four days are added to the total.

Tagged : / / / / / / / / /