PPS U5 Module2 (A)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

● The first method __init__() is a special method, which is called class

constructor or initialization method that Python calls when you create a


new instance of this class.

● You declare other class methods like normal functions with the exception
that the first argument to each method is self.
class Student:

def __init__(self, name, age):


self.name = name
self.age = age

def display(self):
print(self.name)
print(self.age)

Output :
Soham
obj = Student("Soham", 20)
20
obj.display()
class employee:
def __init__(self, name,age,salary,address):
self.name = name
obj = employee("Soham", 20, 30000,'Nashik')
self.age = age
obj.display()
self.salary = salary
self.address = address
Output :

Soham
def display(self): 20
print(self.name) 30000
Nashik
print(self.age)
print(self.salary)
print(self.address)
Inheritance : Inheritance is the capability of one class to derive or inherit the
properties from another class.

The child class acquires the properties and can access all the data members and
functions defined in the parent class. A child class can also provide its specific
implementation to the functions of the parent class.

Parent / Old class / Base class / Super

Child / new class / Sub class / Derived class


# Example of inheritance

class Parent():

def first(self):
print('first function')

# This is child class


class Child(Parent):
Output :
def second(self):
print('second function') first function
second function
obj = Child()
obj.first()
obj.second()
class Father:
def speak(self):
print("Father Speaking")

# child class kids inherits the base class Father


class kids(Father):
def sing(self):
print("Kids singing") Output :

Kids singing
obj = kids() Father Speaking
obj.sing()
obj.speak()
class Addition:
def add(self): obj = Show()

a=10 obj.display()

b=20 obj.add()

sum=a+b
print("Sum=",sum) Output :

This is Addition
#child class show inherits the base class Addition ('Sum=', 30)
class Show(Addition):
def display(self):
print("This is Addition")

You might also like