Flux rss

OOP - Polymorphism

 

Definition of polymorphism

The word polymorphism comes from Greek and means having several different forms. This is one of the essential concepts of object-oriented programming. Where inheritance is related to classes and (their hierarchy), polymorphism is related to object methods.

In general there are three types of polymorphism:

The different types of polymorphism

We will now attempt to describe more precisely the above types, but be warned, many people get confused when trying to understand the differences between the three types.

Overloading polymorphism

Overloading polymorphism is where functions of the same name exist, with similar functionality, in classes which are completely independent of each other (these do not have to be children of the object class). For example, the complex class, the image class and the link class may each have the function "display". This means that we do not need to worry about what type of object we are dealing with if all we want to do is display it on the screen.

Overloading polymorphism therefore allows us to define operators whose behaviour will vary depending on the parameters that are applied to them. Therefore it is possible, for example, to add the + operator and make it behave differently according to whether it refers to an operation between two integers (addition) or between two character strings (concatenation).

Parametric polymorphism

Parametric polymorphism is the ability to define several functions using the same name, but using different parameters (name and/or type). Parametric polymorphism automatically selects the correct method to be adopted according to the type of data passed in the parameter.

We can therefore define several addition() methods using the same name which calculates a sum of values.

  • The int addition(int,int) method would return the sum of two integers.
  • The float addition(float, float) would return the sum of two floats.
  • The char addition(char, char) would result as the sum of two characters as defined by the author.
  • etc.

A signature is the name and type (static) given to the arguments of a function. Therefore it is a method's signature which determines what is called on.

Inclusion polymorphism

The ability to redefine a method in classes that are inherited from a base class is called specialisation. One can therefore call on an object's method without having to know its intrinsic type: this is inclusion polymorphism. This makes it possible to disregard the specialised class details of an object family, by masking them with a common interface (this being the basic class).

Imagine a game of chess, with the objects king, queen, bishop, knight, rook and pawn, each inheriting the piece object.
The movement method could, using inclusion polymorphism, make the corresponding move according to the object class that is called on. This therefore allows the program to perform piece.movement without having to be concerned with each piece's class.

Last update on Thursday October 16, 2008 02:43:13 PM.

This document entitled « OOP - Polymorphism » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.

Results for OOP Polymorphism

Erro na pagina Olá, Desde há mais de quatro dias que ao cocolcar o url do hi5 http://www.hi5.com/, me dá o seguinte erro: Oops! We were unable to fulfill your request. Sorry for the inconvenience, but we experienced an error fulfilling your request. Go back to... en.kioskea.net/forum/affich-26610-erro-na-pagina
OOP - Objects Object-oriented programming consists of arranging data in modular sets of elements of real world information (called a domain. These data elements are called objects. This data is grouped according to the main real world characteristics of those... en.kioskea.net/poo/objet.php3
OOP - Object class and instance A class is the structure of an object, meaning the definition of all items that an object is made up of. An object is therefore the "result" of a class. In reality, an object is an instance of a class, which is why can use interchange the terms... en.kioskea.net/poo/classe-instance.php3

Results for OOP Polymorphism

Oopsie! Monitor fell....the actual storyHello, okay. that original message was blank. lol. Anyway, somehow my 17" Planar Flat Panel tipped off the front of my desk today and was hanging by the cable and power cord... I immediately picked it up and turned it on and it said NO SIGNAL. I... en.kioskea.net/forum/affich-38768-oopsie-monitor-fell-the-actual-story

Results for OOP Polymorphism

Introduction to object-oriented programming Extract from "Understanding Active X and OLE" by David Chappel "In the last 35 years, designers of computer hardware have gone from designing machines the size of a hangar to thin notebooks based on minuscule microprocessors. In the same period,... en.kioskea.net/poo/poointro.php3
Introduction to object-oriented programming - Encapsulation Encapsulation is a way of organising data and methods into a structure by concealing the the way the object is implemented, i.e. preventing access to data by any means other than those specified. Encapsulation therefore guarantees the integrity of... en.kioskea.net/poo/encapsul.php3