Polymorphism in OOP Programming

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

Polymorphism, a core OOP principle, enables an action or method to behave differently depending on the object it operates on. It manifests in three primary forms: overloading, overriding, and dynamic method binding. Overloading involves methods sharing a name but differing in parameter count or types, allowing context-specific behavior. Overriding occurs when a subclass redefines a method with an identical signature, ensuring the subclass's implementation is used. Dynamic method binding further extends this by resolving method calls to the correct subclass implementation at runtime, even when referenced through a parent type.

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloadingoverriding and dynamic method binding are three types of polymorphism.

Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter you control the desired effect.

Overridden methods are methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used.

Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at runtime. As an example assume that three subclasses (Cow, Dog and Snake) have been created based on the Animal abstract class, each having their own speak() method. Although each method reference is to an Animal (but no animal objects exist), the program is will resolve the correct method reference at runtime.

JAVA OOP POLYMORPHISM OVERLOADING OVERRI

  RELATED

  COMMENTS

0

No comment for this article.