Inheritance-Object Oriented Programming (OOPs) concept in python

 

Inheritance 

                 Inheritance is the most important aspect of object-oriented programming. It provides the re-usability of the code.
                Parent class :It is base class ,child class :another class and also called child class

Types of Inheritance:

               Single inheritance
                Multilevel inheritance
                Multiple inheritance
                Hierarchical inheritance
                Hybrid inheritance

The Single Inheritance

                Single Inheritance is the most important aspect of object-oriented programming.
                Single Inheritance have single parant and single class.



Single Inheritance


Example:

class Employee: --------------------------------------------------(parent class)
    def emp_id(self,id):
        print("employee id is",id)
    def emp_name(self,name):
        print("employee name is",name)
class company(Employee):--------------------------------------(child class)
    def com_id(self,id):
        print("company id is",id)
    def com_name(self,name):
        print("company name is",name)

c=company()
c.emp_id(9999)
c.emp_name("antrow")
c.com_id(1)
c.com_name("google")

Multilevel inheritance

                Multilevel inheritance have single parent class and multi child class.
Multilevel Inheritance


Example:

class Employee:------------------------------------------(Parent class)
    def emp_id(self,id):
        print("employee id is",id)
    def emp_name(self,name):
        print("employee name is",name)
class company(Employee):-----------------------------(Child class)
    def com_id(self,id):
        print("company id is",id)
    def com_name(self,name):
        print("company name is",name)
class client(company):-----------------------------------(Child class)
    def cl_name(self,name):
        print("client name is",name)

cl=client()
cl.emp_id(9999)
cl.emp_name("antrow")
cl.com_id(1)
cl.com_name("google")
cl.cl_name("mano")

Multiple inheritance

                Multiple inheritance have multi parent class and single class.

Multiple Inheritance

Example:

class Employee:--------------------------------------(Parent class)
    def emp_id(self,id):
        print("employee id is",id)
    def emp_name(self,name):
        print("employee name is",name)
class company:----------------------------------------(parent class)
    def com_id(self,id):
        print("company id is",id)
    def com_name(self,name):
        print("company name is",name)
class client(Employee,company):-------------------(Child class)
    def cl_name(self,name):
        print("client name is",name)

cl=client()
cl.emp_id(9999)
cl.emp_name("antrow")
cl.com_id(1)
cl.com_name("google")
cl.cl_name("mano")


Hierarchical inheritance:

                Hierarchical inheritance have single parent class and multi child class


Hierarchical Inheritance

Example:


class Employee:----------------------------------------(parent class)
    def emp_id(self,id):
        print("employee id is",id)
    def emp_name(self,name):
        print("employee name is",name)
class company(Employee):---------------------------(child class)
    def com_id(self,id):
        print("company id is",id)
    def com_name(self,name):
        print("company name is",name)

c=company()
c.emp_id(999)
c.emp_name("antrow")
c.com_id(1)
c.com_name("google")

class client(Employee):-------------------------------(child class)
    def cl_name(self,name):
        print("client name is",name)


cl=client()
cl.emp_id(999)
cl.emp_name("antrow")
cl.cl_name("mano")


Hybrid inheritance:

                Hybrid inheritance have two parent class and multi child class in our example.

Hybrid Inheritance



Example:

class A:----------------------------------------------(parent class)
    def m1(self):
        print("hey")
class B:-----------------------------------------------(parent class)
    def m2(self):
        print("hi")
class C(A,B):
    def m3(self):
        print("antrow")

class D(C):-------------------------------------------(child class)
    def m4(self):
        print("mano")

b=D()
b.m1()
b.m2()
b.m3()
b.m4()

class E(C):-------------------------------------------(child class)
    def m5(self):
        print("python")

b=E()
b.m1()
b.m2()
b.m3()
b.m5()

Comments

Popular posts from this blog

Python if....elif....else statements

What is InnoDB - Why InnoDB use - ACID properties