Polymorphism, Encapsulation and Inheritance in Python Programming
Suvankar Maity
Investment Banking & Financial Analyst Enthusiast | Ex-Data Scientist | Creating impactful business solutions with actionable data insights | Sports Geek ??
Polymorphism, encapsulation and inheritance are three important concepts in object-oriented programming (OOP) that allow us to create and use different types of objects in Python. Let me explain them in a simple way so that a kid can also understand.
1?? Polymorphism: Polymorphism means that we can use the same name for different things in different situations. For example, imagine that you have a toy car and a toy truck. They both have wheels, doors, windows and seats, but they are different types of vehicles. You can call them both cars, but they are not exactly the same. This is polymorphism: we can use the same name for different things depending on what we want to do with them.
2?? Encapsulation: Encapsulation means that we can hide some of the details of an object from other objects. For example, imagine that you have a secret diary where you write down your thoughts and feelings. You don’t want anyone else to read your diary, so you keep it locked in a drawer or a safe. You only show your diary to yourself or someone you trust. This is encapsulation: we can hide some of the information about an object from other objects. In Python, we use encapsulation to keep some information hidden and safe, only letting it out in the right way.
领英推荐
3?? Inheritance: Inheritance means that we can create new types of objects based on existing ones. For example, imagine that you have a pet dog and a pet cat. They both have fur, ears, eyes and tails, but they are different kinds of animals. You can create a new type of animal called a pet bird by inheriting from the dog and cat classes. This means that the pet bird class will have all the features of the dog and cat classes plus some new features like wings and feathers. This is inheritance: we can create new types of objects based on existing ones.