Q:

class inheritance 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
# =============================================================================
# Inhertance
# =============================================================================
class A:
    def feature1(self):
        print('Feature 1 in process...')
    def feature2(self):
        print('Feature 2 in process...')       #Pt.1
        
class B:
    def feature3(self):
        print('Feature 3 in process...')
    def feature4(self):
        print ('Feature 4 in process...')
        
a1 = A() 

a1.feature1()
a1.feature2()

a2 = B()

a2.feature3()
a2.feature4()
# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE
        
# WITH THE USE OF INHERITANCE IS BELOW
class A:
    def feature1(self):
        print('Feature 1 in process...')    
    def feature2(self):
        print('Feature 2 in process...')
        
class B(A):
    def feature3(self):
        print('Feature 3 in process...')    # Pt.2
    def feature4(self):
        print ('Feature 4 in process...')
        
a1 = A() 

a1.feature1()
a1.feature2()

a2 = B()

a2.feature3()
a2.feature4()


# NOW TO CHECK OUT THE DIFFERENCE BETWEEN Pt.1
# AND Pt.2 TRY RUNNIG THE CODE ON THE BASIS OF
# INHERITANCE, IN OTHER WORDS TRY RUNNING ONLY 
# B CLASS IN Pt.2 AND THEN RUN ONLY a2
# YOU WILL SEE A DIFFERENCE IN THE RUNNING OF 
# ONLY a2,,,, IT WILL STILL SHOW THAT FEATURE 3
# AND 4 IS IN PROCESS,, THIS MEANS THAT B IS THE
14
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 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
# =============================================================================
# Inhertance
# =============================================================================
class A:
    def feature1(self):
        print('Feature 1 in process...')
    def feature2(self):
        print('Feature 2 in process...')       #Pt.1
        
class B:
    def feature3(self):
        print('Feature 3 in process...')
    def feature4(self):
        print ('Feature 4 in process...')
        
a1 = A() 

a1.feature1()
a1.feature2()

a2 = B()

a2.feature3()
a2.feature4()
# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE
        
# WITH THE USE OF INHERITANCE IS BELOW
class A:
    def feature1(self):
        print('Feature 1 in process...')    
    def feature2(self):
        print('Feature 2 in process...')
        
class B(A):
    def feature3(self):
        print('Feature 3 in process...')    # Pt.2
    def feature4(self):
        print ('Feature 4 in process...')
        
a1 = A() 

a1.feature1()
a1.feature2()

a2 = B()

a2.feature3()
a2.feature4()
0
class Bird():
        def eat(self):
                print ("eating")
 
class Sparrow(Bird):
        def sound(self):
                print ("ChiChi!")
 
birdobj = Sparrow()
birdobj.eat()
birdobj.sound()
-1

New to Communities?

Join the community