-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodOver-ride.py
51 lines (41 loc) · 1.79 KB
/
MethodOver-ride.py
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
#Method over riding
# class student:
# def __init__(self):
# self.name="Waris-Hayat"
# self.rollnum="SP-21368"
# self.section="4A-Afternoon"
# def __str__(self):
# return f"Name is {self.name} and class is {self.rollnum} and section is {self.section}"
# class University(student):
# def __init__(self,):
# super().__init__()
# self.teacher_name="Syed Taha"
# self.marks="3.8CGPA"
# def __str__(self): #Method will override here
# return f"Name is {self.name} and rollnum {self.rollnum} and section is {self.section} teacher name is{self.teacher_name} and total marks are {self.marks}"
# waris=University()
# print(waris)
#Consider a base class Vehicle with a method fuel_efficiency() that calculates and
# returns the fuel efficiency of the vehicle. Create two subclasses Car and Motorcycle
# that inherit from Vehicle. Override the fuel_efficiency() method in both subclasses to
# provide specific implementations for cars and motorcycles. Additionally, create instances
# of both subclasses and demonstrate the use of the overridden method.
class vehical:
def __init__(self):
self.feulEfficeny=10.5
def fuel_efficiency(self):
return self.feulEfficeny
class car(vehical): #class car which inherit vehical
def __init__(self):
self.feulEfficeny=10.9
def fuel_efficiency(self):
return self.feulEfficeny
class motercycle(vehical): #class motercycle which inherit vehical
def __init__(self):
self.feulEfficeny=10.1
def fuel_efficiency(self):
return self.feulEfficeny
moto=car()
print("The fuel efficency of moto:",moto.fuel_efficiency())
biker=motercycle()
print("The fuel efficency of moto:",biker.fuel_efficiency())