Free Ebook cover Basic to Advanced Logic Programming Course

Basic to Advanced Logic Programming Course

New course

50 pages

Inheritance and Polymorphism

Capítulo 21

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Inheritance and polymorphism are two of the four fundamental pillars of object-oriented programming (OOP), along with abstraction and encapsulation. Both are profound concepts that allow programmers to write efficient and reusable code.

Inheritance

Inheritance is a mechanism that allows one class to derive properties and behavior from another class. The class from which a class inherits is known as the base class, parent class, or superclass. The class that inherits is known as a derived class, child class, or subclass.

Inheritance is a form of code reuse. It allows programmers to create classes that build on top of existing classes. When a class inherits from another class, it inherits all public and protected members of the base class. This means that a subclass may have access to more information than is actually needed, which can lead to encapsulation issues.

For example, suppose we have a class 'Animal' with properties like 'name', 'age' and methods like 'eat' and 'sleep'. Now, if we want to create a 'Dog' class, instead of rewriting all these properties and methods, we can simply make the 'Dog' class inherit from the 'Animal' class. This gives us immediate access to all the methods and properties of the 'Animal' class in the 'Dog' class.

Polymorphism

Polymorphism is a concept that allows objects of different classes to be treated as objects of a common class. This is especially useful when working with inherited classes, as it allows subclass objects to be treated like superclass objects.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Polymorphism allows the same method name to be used for methods in different classes. This means that we can have a method with the same name in different classes, but with different behavior. This is known as compile-time polymorphism or method overloading.

For example, we can have a 'draw' method in the 'Shape' class and the same 'draw' method in the 'Circle' class. The 'draw' method in the 'Shape' class may have a generic implementation for drawing a shape, while the 'draw' method in the 'Circle' class may have a specific implementation for drawing a circle.

Polymorphism also allows the same method to be used in a base class and its subclasses, but with different behavior. This is known as runtime polymorphism or method substitution.

For example, we can have a 'speak' method in the 'Animal' class and the same 'speak' method in the 'Dog' class. The 'speak' method in the 'Animal' class may have a generic implementation for making a sound, while the 'speak' method in the 'Dog' class may have a specific implementation for barking.

Conclusion

Inheritance and polymorphism are powerful concepts that allow programmers to write efficient and reusable code. They are fundamental to object-oriented programming and are widely used in most modern programming languages.

By understanding and applying these concepts, programmers can improve code organization, make code more readable and maintainable, and enjoy the benefits of code reuse and encapsulation.

Now answer the exercise about the content:

What are the four fundamental pillars of object-oriented programming (OOP)?

You are right! Congratulations, now go to the next page

You missed! Try again.

The four fundamental pillars of object-oriented programming (OOP) are Inheritance, Polymorphism, Abstraction, and Encapsulation. These concepts allow developers to write efficient, reusable, and organized code. Inheritance lets classes derive properties from other classes, while polymorphism allows for treating objects of different classes as instances of a common class. Abstraction simplifies complex logic, and encapsulation protects the internal state of objects.

Next chapter

Encapsulation and abstraction

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.