Skip to content

Commit 86999dc

Browse files
Added Inheritance Programs
1 parent 1c10815 commit 86999dc

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

Lecture_16/Class Notes/4_Inheritance.MD

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ True
6969
* **Hierarchial Inheritance :** More than one derived class can be created from a single base.
7070
* **Hybrid Inheritance :** This forms combines more than one form if inheritance. Basically, it is a blend of more than one type of inheritance.
7171

72+
## **Single Inheritance**
73+
74+
```py
75+
# Base class
76+
class Animal:
77+
def speak(self):
78+
return "The animal makes a sound."
79+
80+
# Derived class
81+
class Dog(Animal):
82+
def speak(self):
83+
return "The dog barks."
84+
85+
# Create an instance of the Dog class
86+
my_dog = Dog()
87+
88+
# Call the speak method
89+
print(my_dog.speak()) # Output: The dog barks.
90+
```
91+
7292
## **Multiple Inheritance**
7393
```py
7494
# When a child class inherits from multiple parent classes, it is called Multiple Inheritance.
@@ -138,6 +158,71 @@ Address: Boston
138158
"""
139159
```
140160

161+
## **Hierarchial Inheritance**
162+
163+
```py
164+
# Base class
165+
class Animal:
166+
def speak(self):
167+
return "The animal makes a sound."
168+
169+
# Derived class 1
170+
class Dog(Animal):
171+
def speak(self):
172+
return "The dog barks."
173+
174+
# Derived class 2
175+
class Cat(Animal):
176+
def speak(self):
177+
return "The cat meows."
178+
179+
# Derived class 3
180+
class Cow(Animal):
181+
def speak(self):
182+
return "The cow moos."
183+
184+
# Create instances of the derived classes
185+
my_dog = Dog()
186+
my_cat = Cat()
187+
my_cow = Cow()
188+
189+
# Call the speak method for each instance
190+
print(my_dog.speak()) # Output: The dog barks.
191+
print(my_cat.speak()) # Output: The cat meows.
192+
print(my_cow.speak()) # Output: The cow moos.
193+
```
194+
195+
## **Hybrid Inheritance**
196+
197+
```py
198+
# Base class
199+
class Animal:
200+
def speak(self):
201+
return "The animal makes a sound."
202+
203+
# Derived class 1
204+
class Dog(Animal):
205+
def speak(self):
206+
return "The dog barks."
207+
208+
# Derived class 2
209+
class Cat(Animal):
210+
def speak(self):
211+
return "The cat meows."
212+
213+
# Derived class 3 (inherits from Dog and Cat)
214+
class HybridAnimal(Dog, Cat):
215+
def hybrid_speak(self):
216+
return f"{self.speak()} and {Cat.speak(self)}"
217+
218+
# Create an instance of the HybridAnimal class
219+
my_hybrid = HybridAnimal()
220+
221+
# Call the methods
222+
print(my_hybrid.hybrid_speak()) # Output: The dog barks. and The cat meows.
223+
```
224+
225+
141226
## **Method Chaining**
142227
Calling multiple methods automatically each call performs an actions on the same object and returns self.
143228
```py
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Base class
2+
class Animal:
3+
def speak(self):
4+
return "The animal makes a sound."
5+
6+
# Derived class
7+
class Dog(Animal):
8+
def speak(self):
9+
return "The dog barks."
10+
11+
# Create an instance of the Dog class
12+
my_dog = Dog()
13+
14+
# Call the speak method
15+
print(my_dog.speak()) # Output: The dog barks.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# When a child class inherits from multiple parent classes, it is called Multiple Inheritance.
2+
3+
class Base1(object):
4+
# constructors
5+
def __init__(self):
6+
self.str1 = "USA"
7+
print("Base1 Class")
8+
class Base2(object):
9+
# constructors
10+
def __init__(self):
11+
self.str2 = "Germany"
12+
print("Base2 Class")
13+
class Derived(Base1, Base2):
14+
def __init__(self):
15+
# Calling constructors of base classes
16+
Base1.__init__(self)
17+
Base2.__init__(self)
18+
print("Derived Class")
19+
def printStr(self):
20+
print("Countries:", self.str1, "", self.str2)
21+
obj = Derived()
22+
obj.printStr()
23+
24+
"""
25+
Output:
26+
Base1 Class
27+
Base2 Class
28+
Derived Class
29+
Countries: USA Germany
30+
"""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# When we have a child and grandchild relationship. This means that a child class will inherit from its parent class, which in turn is inheriting from its parent class.
2+
class Base(object):
3+
# constructor
4+
def __init__(self, name):
5+
self.name = name
6+
def getName(self):
7+
return self.name
8+
9+
class Child(Base):
10+
def __init__(self,name,age):
11+
Base.__init__(self,name)
12+
self.age = age
13+
def getAge(self):
14+
self.age = age
15+
16+
class grandChild(Child):
17+
def __init__(self, name, age, address):
18+
Child.__init__(self,name,age)
19+
self.address = address
20+
def getAddress(self):
21+
return self.address
22+
objGrandChild = grandChild("Richard", 23, "Boston")
23+
print("Name:", objGrandChild.name)
24+
print("Age:", objGrandChild.age)
25+
print("Address:", objGrandChild.address)
26+
27+
"""
28+
Output:
29+
Name: Richard
30+
Age: 23
31+
Address: Boston
32+
"""
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Base class
2+
class Animal:
3+
def speak(self):
4+
return "The animal makes a sound."
5+
6+
# Derived class 1
7+
class Dog(Animal):
8+
def speak(self):
9+
return "The dog barks."
10+
11+
# Derived class 2
12+
class Cat(Animal):
13+
def speak(self):
14+
return "The cat meows."
15+
16+
# Derived class 3
17+
class Cow(Animal):
18+
def speak(self):
19+
return "The cow moos."
20+
21+
# Create instances of the derived classes
22+
my_dog = Dog()
23+
my_cat = Cat()
24+
my_cow = Cow()
25+
26+
# Call the speak method for each instance
27+
print(my_dog.speak()) # Output: The dog barks.
28+
print(my_cat.speak()) # Output: The cat meows.
29+
print(my_cow.speak()) # Output: The cow moos.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Base class
2+
class Animal:
3+
def speak(self):
4+
return "The animal makes a sound."
5+
6+
# Derived class 1
7+
class Dog(Animal):
8+
def speak(self):
9+
return "The dog barks."
10+
11+
# Derived class 2
12+
class Cat(Animal):
13+
def speak(self):
14+
return "The cat meows."
15+
16+
# Derived class 3 (inherits from Dog and Cat)
17+
class HybridAnimal(Dog, Cat):
18+
def hybrid_speak(self):
19+
return f"{self.speak()} and {Cat.speak(self)}"
20+
21+
# Create an instance of the HybridAnimal class
22+
my_hybrid = HybridAnimal()
23+
24+
# Call the methods
25+
print(my_hybrid.hybrid_speak()) # Output: The dog barks. and The cat meows.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Car:
2+
def turn_on(self):
3+
print("You started the engine")
4+
return self
5+
def drive(self):
6+
print("You drove the car")
7+
return self
8+
def brake(self):
9+
print("You applied the brake on the Car")
10+
return self
11+
def turn_off(self):
12+
print("You turned off the engine")
13+
return self
14+
car = Car()
15+
print("Without Method Chaining: ")
16+
car.turn_on()
17+
car.drive()
18+
car.brake()
19+
car.turn_off()
20+
21+
# Method Chaining
22+
print("\nWith Method Chaining: ")
23+
car.turn_on().drive()
24+
car.brake().turn_off()
25+
car.turn_on().drive().brake().turn_off()
26+
27+
"""
28+
Output:
29+
Without Method Chaining:
30+
You started the engine
31+
You drove the car
32+
You applied the brake on the Car
33+
You turned off the engine
34+
35+
With Method Chaining:
36+
You started the engine
37+
You drove the car
38+
You applied the brake on the Car
39+
You turned off the engine
40+
You started the engine
41+
You drove the car
42+
You applied the brake on the Car
43+
You turned off the engine
44+
"""

0 commit comments

Comments
 (0)