Polymorphism, Polymorphism Implementation Using Interfaces...


Thanks to polymorphism, the same open interfaces can be implemented in different ways in different classes. In other words, polymorphism allows you to call methods and properties of an object, regardless of their implementation. For example, the object class of the Driver interacts with the object of the Vehicle class through an open interface. If another object, such as Truck or Racing_car, supports such an open interface, the object of the class Driver will be able to interact with them (manage them), regardless of the differences in the implementation of the interface. The main approaches to the implementation of polymorphism are two: through interfaces and through inheritance, which will be discussed further below.

Implementing Polymorphism Using Interfaces

An interface is an agreement that defines a set of public methods implemented by the class. The interface defines a list of methods of the class, but does not say anything about their implementation. In the object it is possible to implement several interfaces, and the same interface can be implemented in different classes. For example, you can describe the management interface for some objects:

// interface names usually start with the letter I interface IDrivable {int Go (...); float Rotate (...); bool Stop (..);


If the class implements an interface, then all methods of this interface should be described in it. For example: class Automobile: Transport, IDrivable {

int Go (...) {);

float Rotate (...) {};

bool Stop (..) {};

// description of other elements ...


Any objects in which some interface is implemented, are able to interact with each other with its help. The IDrivable interface can also be implemented in other classes, for example, such as Truck, Fork-lift truck or Boat. As a result, these objects will be able to interact with the object of the class Driver. The class object The driver is completely ignorant about the implementation of the interface with which he interacts, he knows only the interface itself. The implementation of polymorphism using interfaces is discussed in detail in Sec. 6.3.

Implementing polymorphism through inheritance

The derived classes retain all the characteristics of their base classes and are able to interact with other objects under the guise of instances of the base class , ie, variables of the base type can be assigned references to derived class objects. For example:

The car myAuto; // this is a variable, not a class object! Sport_car sportAuto = new Sport_car ();

// can be assigned, because there is inheritance myAuto = sportAuto;

In this case, you can work with a derived class object, as if it were a base class object. The implementation of polymorphism through inheritance is discussed in more detail in Sec. 5.2.

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)