what is encapsulation, inheritance, and polymorphism?
ELI5: The 4 major principles of Object Oriented Programming: Encapsulation, Abstraction, Inheritance, Polymorphism.
What's the deal with abstraction and polymorphism in python?
ELI5: OOP: Inheritance, encapsulation, abstraction and polymorphism. Using animals as examples.
Inheritance is much like evolution. Different breeds of dog all come from the same wolf-like ancestor. They inherit their properties (fields, like number of legs or the presence of fur), and behaviours (barking, eating). Sometimes a particular breed of dog can extend the archetype it inherits from, such as the property of having curly hair (poodles) or the behaviour of sniffing things out on command (bloodhound). However, if a dog breed loses the ability to perform some specific action of its ancestor, it breaks the Liskov Substitution Principle, and is regarded as a badly designed breed.
Encapsulation is the business of tying related behaviours and state together, and not exposing the internals to the outside world. I can't think of an animal metaphor, but a good example is a microwave. How do you operate a microwave oven? Do you open up the case, attach some wires and apply voltages to certain pins? Of course not: you'd have to know lots about the design of the microwave (meaning learning it over and over again for each new oven), and as an outsider, you might mess things up or leave the microwave in a contradictory state (you turned the grill off but kept the light on). No, you interact with the microwave via an interface. It provides a simple mechanism that only requires you to know the high level details of what you want. With an interface, the innards of a microwave can be upgraded without causing problems for wannabe microwave chefs - so long as the contract of the interface is maintained, you will keep being able to use the same old commands as before. This wouldn't have been possible without encapsulation.
Abstraction is usually used to mean classes that are only implemented by their children. Thing of the wolf-dog ancestor: it's extinct, but its properties and behaviours are still manifested in modern instances of everyday dogs, and those dogs still inherit from their parent, even though no real wolf-dog lives in the world today.
Polymorphism is about properties and particularly behaviours being overridden. So think of how a poodle's yap overrides the wolf-dog's bark. The poodle has a special case of the greater bark() method. This allows us to specify and switch different kind of dogs forndiffereent purposes, but still use the same interface with them.
I'd wrote more, but I'm typing on a phone. Good luck with your learning - I'm in a similar boat to yourself!
More on reddit.comWhat is encapsulation in Java
What is an abstraction in OOP
What is the full concept of OOP
I have a basic understanding of inheritance but the other two I donโt understand. What is the purpose of there functionality and when would I use them?