-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.py
More file actions
62 lines (46 loc) · 1.55 KB
/
inheritance.py
File metadata and controls
62 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class SoftwareDeveloper:
def __init__(self, name, programming_language):
self.name = name
self.id = id(self)
self.intro = (self.name, self.id, programming_language)
class SoftwareEngineer(SoftwareDeveloper):
def __call__(self):
return f"{self.name} is coding in {self.intro[2]}"
class SoftwareTester(SoftwareDeveloper):
def __call__(self):
return f"{self.name} is testing the software in {self.intro[2]}"
p2 = SoftwareEngineer("John", "Java")
p3 = SoftwareTester("Bob", "Java")
print(p2.intro)
print(p2())
print(p3.intro)
print(p3())
# Single inheritance Base Class
class Vehicle:
def start(self):
print("The vehicle is starting.")
# Subclass
class Car(Vehicle):
def honk(self):
print("The car is honking. Beep beep!")
# Creating Instance
car1 = Car()
car1.start() # Output: The vehicle is starting.
car1.honk() # Output: The car is honking. Beep beep!
# Multiple Inheritance Example# Base Class 1
class Electric:
def charge(self):
print("The vehicle is charging.")
# Base Class 2
class Engine:
def fuel(self):
print("The vehicle is refueling.")
# Subclass inheriting from both
class HybridCar(Electric, Engine):
def drive(self):
print("The hybrid car is driving.")
# Creating Instance
hybrid_car = HybridCar()
hybrid_car.charge() # Output: The vehicle is charging.
hybrid_car.fuel() # Output: The vehicle is refueling.
hybrid_car.drive() # Output: The hybrid car is driving.