1# =============================================================================
2# Inhertance
3# =============================================================================
4class A:
5 def feature1(self):
6 print('Feature 1 in process...')
7 def feature2(self):
8 print('Feature 2 in process...') #Pt.1
9
10class B:
11 def feature3(self):
12 print('Feature 3 in process...')
13 def feature4(self):
14 print ('Feature 4 in process...')
15
16a1 = A()
17
18a1.feature1()
19a1.feature2()
20
21a2 = B()
22
23a2.feature3()
24a2.feature4()
25# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE
26
27# WITH THE USE OF INHERITANCE IS BELOW
28class A:
29 def feature1(self):
30 print('Feature 1 in process...')
31 def feature2(self):
32 print('Feature 2 in process...')
33
34class B(A):
35 def feature3(self):
36 print('Feature 3 in process...') # Pt.2
37 def feature4(self):
38 print ('Feature 4 in process...')
39
40a1 = A()
41
42a1.feature1()
43a1.feature2()
44
45a2 = B()
46
47a2.feature3()
48a2.feature4()
49
50
51# NOW TO CHECK OUT THE DIFFERENCE BETWEEN Pt.1
52# AND Pt.2 TRY RUNNIG THE CODE ON THE BASIS OF
53# INHERITANCE, IN OTHER WORDS TRY RUNNING ONLY
54# B CLASS IN Pt.2 AND THEN RUN ONLY a2
55# YOU WILL SEE A DIFFERENCE IN THE RUNNING OF
56# ONLY a2,,,, IT WILL STILL SHOW THAT FEATURE 3
57# AND 4 IS IN PROCESS,, THIS MEANS THAT B IS THE
1# creating parent class
2class Parent:
3 BloodGroup = 'A'
4 Gender = 'Male'
5 Hobby = 'Chess'
6
7# creating child class
8class Child(Parent): # inheriting parent class
9 BloodGroup = 'A+'
10 Gender = 'Female
11
12 def print_data():
13 print(BloodGroup, Gender, Hobby)
14
15# creating object for child class
16child1 = Child()
17# as child1 inherits it's parent's hobby printed data would be it's parent's
18child1.print_data()
19
1class Person:
2 name = ""
3
4 def __init__(self, personName):
5 self.name = personName
6
7 def showName(self):
8 print(self.name)
9
10class Student(Person): # Student inherits from Person superclass
11 studentClass = ""
12
13 def __init__(self, studentName, studentClass):
14 Person.__init__(self, studentName) # superclass constructor
15 self.studentClass = studentClass # Student class specific
16
17 def getStudentClass(self):
18 return self.studentClass
19
20
21person1 = Person("Dave")
22person1.showName() # Dave
23student1 = Student("Mary", "Maths")
24print(student1.getStudentClass()) # Maths
25student1.showName() # Mary
1class Robot:
2
3 def __init__(self, name):
4 self.name = name
5
6 def say_hi(self):
7 print("Hi, I am " + self.name)
8
9class PhysicianRobot(Robot):
10
11 def say_hi(self):
12 print("Everything will be okay! ")
13 print(self.name + " takes care of you!")
14
15y = PhysicianRobot("James")
16y.say_hi()
17
1class Parent:
2
3 def abc(self):
4 print("Parent")
5
6class LeftChild(Parent):
7
8 def pqr(self):
9 print("Left Child")
10
11class RightChild(Parent):
12
13 def stu(self):
14 print("Right Child")
15
16class GrandChild(LeftChild,RightChild):
17
18 def xyz(self):
19 print("Grand Child")
20
21obj1 = LeftChild()
22obj2 = RightChild()
23obj3 = GrandChild()
24obj1.abc()
25obj2.abc()
26obj3.abc()
1class Robot:
2
3 def __init__(self, name):
4 self.name = name
5
6 def say_hi(self):
7 print("Hi, I am " + self.name)
8
9class PhysicianRobot(Robot):
10 pass
11
12x = Robot("Marvin")
13y = PhysicianRobot("James")
14
15print(x, type(x))
16print(y, type(y))
17
18y.say_hi()
19