Polymorphism

Why Trust Techopedia

What Does Polymorphism Mean?

Polymorphism is an object-oriented programming (OOP) concept that refers to the ability of a variable, function or object to take on multiple forms. A language that features polymorphism allows developers to access objects of different types through the same interface.

Advertisements

A common use of polymorphism in OOP is when a parent class reference is used to refer to a child class object.

Techopedia Explains Polymorphism

In a programming language that exhibits polymorphism, objects of classes belonging to the same hierarchical tree (inherited from a common base class) may possess functions bearing the same name, but each may have different behaviors.

For example, assume there is a base class named Animals from which the subclasses Horse, Fish and Bird are derived. Also assume that the Animals class has a function named Move, which is inherited by all subclasses mentioned. With polymorphism, each subclass may have its own way of implementing the function. So, for example, when the Move function is called in an object of the Horse class, the function might respond by displaying trotting on the screen. On the other hand, when the same function is called in an object of the Fish class, swimming might be displayed on the screen. In the case of a Bird object, it may be flying.

In effect, polymorphism cuts down the developer work because they can create a general class with all the attributes and behaviors they envision for it. When the time comes for the developer to create more specific subclasses with certain unique attributes and behaviors, they can simply alter code in the specific portions where the behaviors differ. All other portions of the code can be left as is.

Advertisements

Related Terms

Margaret Rouse
Technology Specialist
Margaret Rouse
Technology Specialist

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.