Skip to content

Commit 2dd1812

Browse files
committed
Add OOP implementation with Car and ElectricCar classes; include multiple inheritance example
1 parent c1df459 commit 2dd1812

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

OOP/oop.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Car:
2+
def __init__(self, brand, model):
3+
self.brand = brand
4+
self.model = model
5+
6+
def get_brand(self):
7+
return self.brand
8+
9+
def full_name(self):
10+
return f"{self.brand} {self.model}"
11+
12+
def full_type(self):
13+
return "Petrol or Diesel"
14+
15+
@staticmethod
16+
def general_description():
17+
return "Cars are means of transport"
18+
19+
20+
class ElectricCar(Car):
21+
def __init__(self, brand, model, battery_size):
22+
super().__init__(brand, model)
23+
self.battery_size = battery_size
24+
25+
def full_type(self):
26+
return "Electric Charge"
27+
28+
29+
# Object creation and usage
30+
my_tesla = ElectricCar("Tesla", "Model S", "85Kwh")
31+
print(my_tesla.full_name()) # Tesla Model S
32+
print(Car.general_description()) # Cars are means of transport
33+
34+
my_car = Car("Toyota", "Corolla")
35+
print(my_car.brand) # Toyota
36+
print(my_car.model) # Corolla
37+
print(my_car.full_name()) # Toyota Corolla
38+
39+
my_new_car = Car("Tata", "Safari")
40+
print(my_new_car.brand) # Tata
41+
print(my_new_car.model) # Safari
42+
43+
print(isinstance(my_tesla, Car)) # True
44+
print(isinstance(my_tesla, ElectricCar)) # True
45+
46+
47+
# Multiple inheritance example
48+
class Battery:
49+
def battery_info(self):
50+
return "This is a battery"
51+
52+
class Engine:
53+
def engine_info(self):
54+
return "This is an engine"
55+
56+
class ElectricCarTwo(Battery, Engine, Car):
57+
def __init__(self, brand, model):
58+
super().__init__(brand, model)
59+
60+
my_new_tesla = ElectricCarTwo("Tesla", "Model S")
61+
print(my_new_tesla.engine_info()) # This is an engine
62+
print(my_new_tesla.battery_info()) # This is a battery

0 commit comments

Comments
 (0)