Inheritence and Polymorphism
Students
Oscar David Porras ID: 854046
José Antonio Valderrama ID: 846809
Inheritence and Polymorphism
They are mechanisms that are very useful in object-oriented-programming. In this blog, we'll explain you what is these.
Inheritence
What is that?
It's a mechanism in object-oriented-programming, it's useful to reuse code, because if one or more classes are similar, you can write the features in a class and inherit theses features in other class.
In the inheritance there are one or more classes called superclasses, these classes inherit other classes called subclasses, the superclass inherits its attributes and methods.
The Subclasses besides the sperclass' attributes and methods have own methods and could be superclasses of other subclasses.
Types of inheritence
Single Inheritence
The subclass only inherits features from a single superclass, in this type of inheritence the subclasses can add own attributes and methods
Multiple inheritance
In this type of inheritance the subclass inherits from two or more superclasses. Which means that it adopts the features from both classes.
This is a real-world example, a child inherits characteristics of both parents.
Polymorphism
What is that?
It is the ability of an object to take multiple forms, with this you can create code more flexible and adaptable and it allows you to write code that can be used with multiple types of objects.
We inherit superclass methods in inheritance, while polymorphism uses the methods in different forms.
Example
There is a superclass called "Animal", this class have a method to describe the movement of animals.
but each animal movement in different way, you can use te polymorphism to describe the movement for each animal.
-------------------------------------------------------------------------------------------------------------------