Collecting InheritanceIt’s not exactly what you think…Zurich OkorenBlockedUnblockFollowFollowingMay 17Photo by John Schnobrich on UnsplashWe won’t be talking about the money you might be getting when your Great Aunt Gertrude passes away.
Instead we’ll be comparing some differences in behaviors and implementations of inheritance in Object Oriented Programming Languages.
This article assumes that you are at least familiar with Object Oriented Programming (OOP), but if not Alexander Petkov’s article, How to explain object-oriented programming concepts to a 6-year-old, is a great resource to get you up to speed.
Nothing like the CLASS-icsIn Python inheritance is handled through the use of classes.
Meaning that methods and properties are passed down before the creation of an object.
Take a look at the following block of code:In Python classes behave as you would expect them to according to the standard definition of OOP:Classes are the blueprints to make objects.
Meaning that all the methods and properties in the code block do not exist until an object is created (instantiated).
In Python inheritance is implemented simply by passing a class into another class.
The Runner class in this example is inheriting from the Person class above it.
We can tell because the parenthesis in the line, class Runner(Person): indicate that Person, which is another class, is being passed into the Runner class.
Let’s try out these classes.
Here, we created an instance of the Person class and called it Bob.
Here we can see that Bob has a name, can speak and he can also run.
Then, we instantiated the Runner class and made a Runner object.
We then called it Usain after the famous Olympian.
Notice that even though we didn’t declare a speak method in the Runner class, objects we create from the Runner class can still speak .
This is due to the fact that it inherits everything from the Person class including the speak method and the name property.
Method OverridingYou might notice that both Runner and Person have a run method, but they don’t look the same.
That’s because we’re overriding the Person class’s run with a new run specific to the Runner class.
This means objects created from the Runner class have different run methods than those from Person, because Runner objects will use methods specific to the Runner class, before using methods from their parent class.
Again,let’s take a look at another block of code:I don’t know about you, but I think there’s something different going on here.
This is actually utilizing object composition to produce an outcome similar to what you would see in class-based inheritance.
However, instead of passing down methods and properties from classes to sub-classes, object composition allows us to pick and choose components from multiple objects that are not necessarily related and create new objects from those components.
The key difference is that instead of creating templates for sub-classes based on parent classes, we make functions that take components from other objects and create a new object from those components.
Those functions are called factories, just like real factories, they build the same objects over and over again using the same components.
ConclusionThe OOP ecosystem is quite diverse.
Here we saw that even with inheritance, one of the pillars of OOP, there can be very different solutions to tackle the same problem.
Neither one is better than the other, but just know you’re working with very different tools and should be handled differently in order to make the most of either one.
Everyone's talking about it, and that's not strange…alligator.
ioPython InheritanceInheritance is a powerful feature in object oriented programming.
It refers to defining a new class with little or no…www.
comInheritance in Python – GeeksforGeeksInheritance is the capability of one class to derive or inherit the properties from some another class.
The benefits of…www.
org.. More details