Friday, June 23, 2023

Python Inheritance

 

                     INHERITANCE

 

Base Class                                  Derived  Class

feature1                                    feature1

feature2                                    feature2

                                                   feature3

Python Inheritance Syntax

class BaseClass :

     body of base class

class Derivedclass(baseclass):

      body of derived class

For Example

#base or super class

class Person:

    def __init__(self,no,name):

       self.Empno =no

       self.Ename =name

    def dispdata(self):

       print ("Empoyee    no....:",self.Empno )

       print ("Employee   name...:",self.Ename)

#inherited or subclass (note person in bracket)

class Employee (Person):

    def  callme (self):

         print ("I can use attribute of person")

per=Person(101, "ramkumar")

emp =Employee(102,"suresh")

print("- "*25)

per.dispdata()

emp.dispdata()

OUTPUT

Employee Details

-------------------------

Empoyee    no....: 101

Employee   name...: ramkumar

Empoyee    no....: 102

Employee   name...: suresh

>>>

Method Overriding In Python

class rectangle():

          def __init__(self,length,breadth):

                    self.length = length

                    self .breadth = breadth

          def getarea(self):

                    print(self.length*self.breadth,"is area of rectangle")

class square(rectangle):

        def __init__(self,side):

               self.side  =side

               rectangle.__init__(self,side,side)

        def getarea (self):

               print(self.side*self.side,"is area of square")

s  =  square(4)

r  =  rectangle(2,4)

s.getarea ()    #object s execute derived class  getArea() function not base class getArea()

r.getarea()

Python Multiple Inheritence

syntax :

class base1:

      pass

class bass2:

      pass

class multiderived(base1 , base 2):

        pass

Example For Multiple Inheritance

class student(object):

          tot=0;  avg=0.0 ;  res=" "

          def __init__(self,no,na):

                self.stdno=no ;  self.stdname=na

          def stdfun(self):

                print ("student   no:",self.stdno,"\nstudent name:",self.stdname)

class test1(object):

          def __init__(self,m1,m2):

                    self.p1 = m1 ;  self.p2 = m2

          def markdisp1(self):

                    print ("c language  :",self.p1,)

                    print ("c++     :",self.p2)

class test2(object):

          def __init__(self,m3,m4):

                    self.p3=m3;self.p4=m4

          def markdisp2(self):

                    print ("java    :",self.p3)

                    print ("python    :",self.p4 )

 

class result(student,test1,test2):

          def __init__(self,no,na,m1,m2,m3,m4):

          # calling constructors of test1

          # and test2 classes

                    student.__init__(self,no,na)

                    test1.__init__(self,m1,m2)

                    test2.__init__(self,m3,m4)

 

          def calfun(self):

                    self.tot=self.p1+self.p2+self.p3+self.p4

                    self.avg=self.tot/4

                          if self.p1>=40 and self.p2>=40 and self.p3>40 and self.p4>=40:

                              self.res="pass"

                    else:

                              self.res="fail"

 

          def resdisplay(self):

                      print("total    :",self.tot)

                      print("avg    :",self.avg)

                      print("result    :",self.res)

 

ob= result(101,"raj",56,67,78,90)

ob.calfun()

print("-"*25)

ob.stdfun()

print("-"*25)

ob.markdisp1()

ob.markdisp2()

print("-"*25)

ob.resdisplay()

print("-"*25)

output

-------------------------

student   no: 101

student name: raj

-------------------------

c language  : 56

c++     : 67

java    : 78

python    : 90

-------------------------

total    : 291

avg    : 72.75

result    : pass

-------------------------

 

Multilevel Inheritance In Python

 

Syntax

class base:

        pass

class derived1(base):

        pass

class derived2(derived 1):

        pass

here, derived 1 is derived from base, and derived 2 is derived from derived 1.




Example:

class student :

          def getstudent(self):

                    self.name = input("name: ")

                    self.age = input("age: ")

                    self.gender = input("gender: ")

class test(student):

          def getmarks(self):

                    self.stuclass = input("diploma name: ")

                    print("enter the marks of the respective subjects")

                    self.c = int(input("c language: "))

                    self.cplus = int(input("c + +: "))

                    self.java = int(input("java: "))

                    self.python = int(input("python: "))

 

class marks (test):

#method

          def display(self):

                    print("\n\nname: ",self.name)

                    print("age:",self.age)

                    print("gender: ",self.gender)

                    print("diploma name: ",self.stuclass)

                    print("total marks:{}/400 ".format( self.c + self.cplus + self.java + self.python))

m1 = marks()

#call base class method 'getstudent()'

m1.getstudent()

# call first derived class method 'getmarks()'

m1.getmarks()

#call second derived class method 'display()'

m1.display()

        output

>>> Input

name: gubenthiran

age: 27

gender: male

diploma name: HDCA/C

enter the marks of the respective subjects

c language: 69

c + +: 50

java: 79

python: 90

 

Output

name:  gubenthiran

age: 27

gender:  male

diploma name:  HDCA/C

total marks:288/400

0 comments:

Post a Comment