-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritence.py
More file actions
67 lines (44 loc) · 1.51 KB
/
Copy pathinheritence.py
File metadata and controls
67 lines (44 loc) · 1.51 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
63
64
65
66
67
#%% Inheritence
## parent Class
class Car:
def __init__(self, windows, doors, enginetype):
self.windows = windows
self.doors = doors
self.enginetype = enginetype
def drive(self):
print(f"The Person will drive the {self.enginetype} car")
#%%
car1= Car(4,5, "petrol")
car1.drive()
#%% Single Inheritence
class Tesla(Car):
def __init__(self, windows, doors, enginetype, is_selfdriving):
super().__init__(windows, doors, enginetype)
self.is_selfdriving = is_selfdriving
def selfdriving(self):
print(f"Tesla supports self driving: {self.is_selfdriving}")
# Assuming a definition for the Car class is present
tesla1 = Tesla(4, 5, "electric", True)
tesla1.selfdriving()
#%% Multiple Inheritence
# When a class inherits more than one base class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("Subclass must implement this method")
# Base class 2
class Pet:
def __init__(self, owner):
self.owner = owner
# Derived class
class Dog(Animal, Pet):
def __init__(self, name, owner):
Animal.__init__(self, name) # Fixed: added parentheses
Pet.__init__(self, owner) # Fixed: added parentheses
def speak(self):
return f"{self.name} says woof"
# Create an object
dog = Dog("Buddy", "Krish")
print(dog.speak())
print(f"Owner: {dog.owner}")