INHERITANCE IN PYTHON

GOCOURSE

 


Inheritance

      A special feature of Object-Oriented Programming in Java, Inheritance lets programmers create new classes that share some of the attributes of existing classes. Using Inheritance lets us build on previous work without reinventing the wheel.It is the process of creating a new class from existing class.  Base class is parent class and child class is sub class.

They are some types of inheritance such as

           • Single Inheritance 
           • Multi Level Inheritance 
           • Hierarchical Inheritance
           • multiple Inheritance
           • Hybrid Inheritance

Single inheritance:

By allowing a derived class to inherit properties from a single parent class, single inheritance makes it possible to reuse code and add new features to code that already exists.

Program:


  # single inheritance
 
  # Base class
  class Parent:
      def func1(self):
              print("This is parent class.")
  
  # Derived class
 
 
  class Child(Parent):
      def func2(self):
          print("This is child class.")
 
 
  # object creating
  obj = Child()
  obj.func1()
  obj.func2()


Output:


  This is parent class.
  This is child class.

Multiple inheritance:

Multiple inheritances refer to the situation in which a class can be derived from more than one base class. In multiple inheritances, the derived class inherits all base class characteristics.

Program:


  # multiple inheritance
  
  # Base class1
  class student:
      Studentname = ""
 
      def student(self):
               print(self.studentname)
 
  # Base class2
 
 
  class department:
      departmentname = ""
 
      def department(self):
          print(self.departmentname)
 
  # Derived class
 
 
  class college(student, department):
      def student(self):
               print("department :", self.departmentname)
               print("student :", self.studentname)
 
 
  # object creating
  s1 = college()
  s1.departmentname = "computer science"
  s1.studentname = "Ram"
  s1.student()


Output:


  department : computer science
  student : Ram

Multilevel inheritance:

Features of the base class and the derived class are further inherited into the new derived class in multilevel inheritance. A relationship depicting a child and a grandfather is similar to this.

Program:


  # multilevel inheritance
   
  # Base class
 
 
  class name:
 
      def __init__(self, name1):
          self.name1 = name1
 
  # Intermediate class
 
 
  class Hobby(name):
      def __init__(self, Hobby1, name1):
          self.Hobby1 = Hobby1
 
           # invoking constructor of name class
          name.__init__(self, name1)
 
  # Derived class
 
 
  class age(Hobby):
      def __init__(self, age1, Hobby1, name1):
          self.age1 = age1
 
          # invoking constructor of Hobby class
          Hobby.__init__(self, Hobby1, name1)
   
      def print_name(self):
          print(' name :', self.name1)
          print("Hobby :", self.Hobby1)
          print("age :", self.age1)
 
 
  #  Driver code
  s1 = age(20, 'hockey', 'Ram')
  print(s1.name1)
  s1.print_name()


Output:

  Ram
   name : Ram
  Hobby : hockey
  age : 20

 Hierarchical Inheritance:

Hierarchical inheritance refers to the process by which a single base is used to create multiple derived classes. There are two derived classes and a parent class in this program.

Program:


  # Hierarchical inheritance
 
 
  # Base class
  class Parent:
      def func1(self):
          print("best program language")
 
  # Derived class1
 
 
  class Child1(Parent):
      def func2(self):
          print("python")
 
  # Derived class2
 
 
  class Child2(Parent):
      def func3(self):
          print("java")
  
 
  # object creating
  obj1 = Child1()
  obj2 = Child2()
  obj1.func1()
  obj1.func2()
  obj2.func1()
  obj2.func3()


Output:


  best program language
  python
  best program language
  java

Hybrid inheritance:

Hybrid inheritance is inheritance that consists of multiple inheritance types.

Program:


  # hybrid inheritance
 
 
  class website:
      def func1(self):
               print("Gocourse pvt ltd.")
 
 
  class msg1(website):
      def func2(self):
               print("Best online tutorial platform. ")
 
 
  class msg2(website):
      def func3(self):
          print("Easy to learn.")
  
 
  class msg3(msg1, website):
      def func4(self):
               print("become a certified programmer in python.")
 
 
  # object creating
  object = msg3()
  object.func1()
  object.func2()


Output:


  Gocourse pvt ltd.
  Best online tutorial platform.
 



More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send