Inheritance is an important concept of object-oriented programming. The idea is to inherit attributes and methods from one class (parent class) to another (child class). This is quite useful, since sometimes we want to modify a class but keeping some exisitng methods and/or attributes. Let's see some examples.
As we saw in the previous examples, we can change the values of the object attributes directly, but this can be a security issue, since we might want to verify something before actually changing the value of an attribute. This is when encapsulation comes in. Encapsulation allows us to protect the attributes by hindering direct changes. We say that an attribute is encapsulated or is private. In python, we use simple or doble underscore to create a private attribute. Let's take a look at some examples.
Polymorphism allows us to deal with objects that can be from multiple classes that have the same methods but they do different things. Let's see some examples.
- OOP is a fundamental paradigm of programming that allows us to group data and code in the same place.
- A class is the abstraction of an idea or real object whereas an object is an instance of a class.
- Inheritance permits us to recycle code from parent classes. It makes the code cleaner as well.
- Encapsulation protects attributes of the classes.
- Polymorphism facilitates the manipulation of objects from multiple classes.