Inheritence in Python


 Inheritence

     Inheritence is the most important aspect of object oriented programming which simmulates the real world concept of inheritence. It specifies that the child object aquires all the properties and behaviour of the parent class.

    Inheritence is a mechanism, by which one class aquires, all the properties and behaviour of another class. The class whose members are inherited is called the super class (or base class or parent class), and the class that inherits those members is called the sub class(or derived class or child class).

     Inheritence provides Re-usability of code that saves our effort and time.

Syntax : In python a derived class can inherit a base class using the following  syntax.

class Derived class(Base class):

Example

class Account:

        def __init__(self,name,acc_num):

                    self.name=name

                    self.acc_num=acc_num

        def withdraw(self,amount):

                    print("withdrawing amount for", self.acc_num)

        def deposit(self,amount):

                    print("depositing amount for", self.acc_num)

class PrivilegedAccount(Account):

        def scheduleAppointment(self):

                    print("schedule appointment for", self.acc_num)

pa=PrivilegedAccount("guptaji",1001)

pa.withdraw(100)

pa.deposit(500)

pa.scheduleAppointment()

Output

withdrawing amount for 1001

depositing amount for 1001

scheduling appointment for 1001












No comments:

Post a Comment