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 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()