While studying OOP I've encountered the concept of polymorphism. For a beginner understanding polymorphism can be tricky. Now that I have the basic understanding of inheritance, subclassing and method overriding I am still trying to understand how is polymorphism promoting extensilbility?.
-
Software that invokes polymorphic behavior is independent of the object types to which messages are sent as long as those types are in the same inheritance hierarchy. New object types that can respond to existing method calls can be incorporated into a system without requiring modification of the base system, except that client code that instantiates new objects must be modified to accommodate new types. – Ahmad Raza May 14 '19 at 12:40
2 Answers
Well, I'm not an expert in oop, I just read a little of it and this is what I understood. When you declare a variable in strong-typed languages you can't change the type of the object, for example in java when you create an array you have to declare the type of data, once this is done you can't insert any other type of data (maybe just null).
Now, imagine that you have a class called vehicles and its subclasses motorcycle, car, and bicycle, using polymorphism you can relax this restriction of types, now you can insert any subclass object to the array, cars, motorcycles or bicycles.
Other thing you can do is create functions that are capable of accepting all the childs of the supperclass, for example imagine that you have the class parking, using polymorphism you can pass to the function any vehicles objects, you can do parking (car), parking (bus), parking (spaceship), etc.
Another advantage is the possibility of add specfic methods to the child classes, for example you can declare the method drive in the supperclass and specify then in the childs how the drive method works, even you can declare an specfic method in the supperclass execute it, and then execute the child method.
That's all i know, i took oop 2 years ago, and I don't rememeber too much, now I'm learning functional programing and there is polymorphism too XD, and its the same, you can accept diferent kinds of data on functions.

- 33
- 8
Just think about a software unit which exposes a simple interface to the outside world, a method which accepts a super type object as input and invokes some polymorphic method on this super type object.
public void baseUnitMethod(SuperType object) {
object.polymorphicMethod();
}
Now let's assume initially the client code uses only one specific implementation of this SuperType. For the sake of the example I will call it OneSpecificType. Our inheritance hierarchy consists so far of a base SuperType and a OneSpecificType which extends the SuperType and provides its own implementation for this polymorphicMethod(). In the end our client code in its simplest form will probably create a new OneSpecificType and pass it as a parameter to the unit.
SuperType oneSpecific = new OneSpecificType();
baseUnitMethod(oneSpecific);
Polymorphism promotes extensibility means I can introduce other specific type implementations in our inheritance hierarchy and the only required changes to incorporate them in our system are in the client code, no changes are required to our base unit. Our base unit which invokes the polymorphic method is independent of the object types considering of course the fact that all these types are in the same inheritance hierarchy.
With two specific types the client code will result in
SuperType oneSpecific = new OneSpecificType();
baseUnitMethod(oneSpecific);
SuperType secondSpecific = new SecondSpecificType();
baseUnitMethod(secondSpecific);
and the base unit will stay unchanged.

- 1,105
- 6
- 16