Bialy
0
Q:

Inheritance example python

class Student:
    def __init(self, name, school):
        self.name = name
        self.school = school
        self.marks = []
        
    def average(self):
        return sum(self.marks) / len(self.marks)
    
    
class WorkingStudent(Student):                        # WorkingStudent() is a child of Student()
    def __init__(self, name, school, salary):
        super().__init__(name, school)                # parent class of Student()
        self.salary = salary


sarah = WorkingStudent('Sarah', 'Oxford', 10)
print(sarah.salary)
sarah.marks.append(30)
sarah.marks.append(28)
print(sarah.average())
6
# creating parent class
class Parent:
    BloodGroup = 'A'
    Gender = 'Male'
    Hobby = 'Chess'
    
# creating child class
class Child(Parent): # inheriting parent class
    BloodGroup = 'A+'
    Gender = 'Female
    
    def print_data():
        print(BloodGroup, Gender, Hobby)
    
# creating object for child class
child1 = Child()
# as child1 inherits it's parent's hobby printed data would be it's parent's
child1.print_data()
  
3
# Python code to demonstrate how parent constructors 
# are called. 
  
# parent class 
class Person( object ):     
  
        # __init__ is known as the constructor          
        def __init__(self, name, idnumber):    
                self.name = name 
                self.idnumber = idnumber 
        def display(self): 
                print(self.name) 
                print(self.idnumber) 
  
# child class 
class Employee( Person ):            
        def __init__(self, name, idnumber, salary, post): 
                self.salary = salary 
                self.post = post 
  
                # invoking the __init__ of the parent class  
                Person.__init__(self, name, idnumber)  
  
                  
# creation of an object variable or an instance 
a = Employee('Rahul', 886012)     
  
# calling a function of the class Person using its instance 
a.display()  
2
class Parent:

    def abc(self):
        print("Parent")

class LeftChild(Parent):

    def pqr(self):
        print("Left Child")

class RightChild(Parent):

    def stu(self):
        print("Right Child")

class GrandChild(LeftChild,RightChild):

    def xyz(self):
        print("Grand Child")

obj1 = LeftChild()
obj2 = RightChild()
obj3 = GrandChild()
obj1.abc()
obj2.abc()
obj3.abc()
1
class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):

    def say_hi(self):
        print("Everything will be okay! ") 
        print(self.name + " takes care of you!")

y = PhysicianRobot("James")
y.say_hi()
0
class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):
    pass

x = Robot("Marvin")
y = PhysicianRobot("James")

print(x, type(x))
print(y, type(y))

y.say_hi()
0
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")
x.printname()
0
   
# A Python program to demonstrate inheritance  
  
# Base or Super class. Note object in bracket. 
# (Generally, object is made ancestor of all classes) 
# In Python 3.x "class Person" is  
# equivalent to "class Person(object)" 
class Person(object): 
      
    # Constructor 
    def __init__(self, name): 
        self.name = name 
  
    # To get name 
    def getName(self): 
        return self.name 
  
    # To check if this person is employee 
    def isEmployee(self): 
        return False
  
  
# Inherited or Sub class (Note Person in bracket) 
class Employee(Person): 
  
    # Here we return true 
    def isEmployee(self): 
        return True
  
# Driver code 
emp = Person("Geek1")  # An Object of Person 
print(emp.getName(), emp.isEmployee()) 
  
emp = Employee("Geek2") # An Object of Employee 
print(emp.getName(), emp.isEmployee()) 
0
<__main__.Robot object at 0x7fd0080b3ba8> <class '__main__.Robot'>
<__main__.PhysicianRobot object at 0x7fd0080b3b70> <class '__main__.PhysicianRobot'>
Hi, I am James
0

New to Communities?

Join the community