DEV Community

Johnson Ogwuru
Johnson Ogwuru

Posted on

Basic Inheritance with Crazy Python

To be sincere right now, i don't even know how to go about this post, making anyone understand this inheritance stuff is going to be hard nut to crack. So for the likes of @ben and @jess (lol) who has refused to teach us privately what inheritance is all about i would just give you a book definition of inheritance then you can go back to work, but for the rest of us that find it hard understanding inheritance (like me) just chill and follow along with the supposed easy tutorial and examples.

Inheritance is a term used to indicate that one class will get most or all of its features from a parent class. In python to create a class that inherits from another class we would do something like;

class [Name of new Class] ([Name of parent Class]):
    pass
Enter fullscreen mode Exit fullscreen mode

Hmmmmmmm, I'm confused too, but for all that understood, you're free to start going now. Go attend to your businesses. But to all like my friend @ioedeveloper (lol) that have refused to understand this please just stay put, you hear.

Recall the example we have been experimenting with, the one with an Employee class. Suppose a need arise that we need to create a new class Developer.(When that need would arise you would know). A developer in a company is an employee, but a different, but cool to be around kind of employee. Developers though most times like going with cool names of their choosing like @ioedeveloper but does that mean this dude has no fullname?....Nope. Now since a developer also has a fullname like any other employee, that means he shares a particular attribute all other employees share. Now try picturing this in code. Remembering one of the strong points of OOP is code re-usability, would it be cool to write the employee class as follows;

class Developer:
    def __init__(self,first,last):
        self.first = first
        self.last = last
Enter fullscreen mode Exit fullscreen mode

when we already have our employee class to be like this also;

class Employee:
    def __init__(self,first,last):
        self.first = first
        self.last = last
Enter fullscreen mode Exit fullscreen mode

we are in no means re-using code when we do this, now that's where the concept of inheritance comes to play. Since the Employee class has some default attributes and traits our developers class has, we would just inherit the employee class, in essence inheriting those attributes of the Employee class.
So now declaring this class we would do something like this;

class Developer(Employee):
    pass
Enter fullscreen mode Exit fullscreen mode

Doing this is very helpful cos now, we can create sub-classes and get all the functionality of our parent class and then override or add completely new functionality without affecting the parent class in any way.

We created some instances the last post;

emp_1 = Employee('John','Son',50000)
emp_2 = Employee('David','Disu',7000)
Enter fullscreen mode Exit fullscreen mode

But now that we've declared the (cool) developer class, we can also create a developer instance;(more like creating a developer);

dev_1 = Developer('Ben','Halpern',50000)
dev_2 = Developer('David','Disu',2000)
Enter fullscreen mode Exit fullscreen mode

Try printing their emails;

print(dev_1.email)
print(dev_2.email)
Enter fullscreen mode Exit fullscreen mode

On running the program you can see that those two developers where created successfully. Incase you have not been following the post on oop with python, you would need to write the code below before you can continue with declaring this new developer class;

class Employee:
    def __init__(self,first,last,pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + '.' + last + '@company.com'

    def fullname(self):
        return self.first + ' ' +self.last
Enter fullscreen mode Exit fullscreen mode

after writing the above code, you can then continue with declaring the developer class and instantiating the class and printing the created developers mail. What actually happened that made sure the program worked when you ran it;when we created instances of our developers class, the program first looked at our developer class for our (init) method, but not finding it, it worked up the chain of inheritance until it finds it; that chain of inheritance is called the ("method resolution order"). To see the (method resolution order) and attributes the developer class inherited do this;

print(help(Developer))
Enter fullscreen mode Exit fullscreen mode

A developer would have more than just a fullname, he would have his preferred programming language, his preferred IDE. So how do we take care of this extra attribute in our code since our employee class only accepts (firstname,lastname,and pay); to get around this we are going to give our developer class its own init method.

class Developer(Employee):
    def __init__(self,first,last,pay,prog_lang)
Enter fullscreen mode Exit fullscreen mode

Now instead of recopying the first,last and pay arguments,(i.e) all these weird looking things;

self.first = first
self.last = last
self.pay = pay
Enter fullscreen mode Exit fullscreen mode

we would allow the Employee class to handle, the first,last,pay attributes by doing this;

class Developer(Employee):
    def__init__(self,first,last,pay,prog_lang):
        super().__init__(self,first,last,pay)
Enter fullscreen mode Exit fullscreen mode

instead of #super, you can use

Employee().__init__(self,first,last,pay)
Enter fullscreen mode Exit fullscreen mode

so as an assignment, just Google the difference between each(super or the parent class name). Now to add the programming language argument, we would add the following line of code under;

self.prog_lang = prog_lang
Enter fullscreen mode Exit fullscreen mode

So now when we instantiate our developer;it would require that we pass in a programming language alongside the other attributes(first,last,pay);

dev_1 = Developer('John','Son',5000000,'Python')
Enter fullscreen mode Exit fullscreen mode

To make sure it works, print developers email and his programming language;

print(dev_1.email)
print(dev_1.prog_lang)
Enter fullscreen mode Exit fullscreen mode

That's it for today guys. I Lack ideas on how to end this piece, so just guys help me out.

Top comments (3)

Collapse
 
ogwurujohnson profile image
Johnson Ogwuru

I'm happy you learnt something big sir

Collapse
 
joeizang profile image
Joe Izang

Nice, learnt something big.

Collapse
 
arunaa2783 profile image
Arunaa G T

Nice