Created
February 8, 2020 11:38
-
-
Save angwandi/3fffc9fe8d970f3272c8ebc72b9dc80e to your computer and use it in GitHub Desktop.
polymorphism : different forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main(){ | |
Car normal = Car(); | |
print(normal.numberOfSeat); | |
normal.drive(); | |
ElectricCar myTesla = ElectricCar(); | |
myTesla.drive(); | |
myTesla.recharge(); | |
LevitatingCar glide = LevitatingCar(); | |
glide.drive(); | |
SelfDriving self = SelfDriving('1 Hacker Way'); | |
self.drive(); | |
} | |
class Car{ | |
int numberOfSeat = 5; | |
void drive(){ | |
print('Wheels turn.'); | |
} | |
} | |
class ElectricCar extends Car{ | |
int batteryLevel = 100; | |
void recharge(){ | |
batteryLevel = 100; | |
} | |
} | |
class LevitatingCar extends Car{ | |
//polymorphism | |
@override | |
void drive(){ | |
print('Glide forward.'); | |
} | |
} | |
class SelfDriving extends Car{ | |
String destination; | |
SelfDriving(String userSetDestination){ | |
destination = userSetDestination; | |
} | |
//polymorphism: inherit and change | |
@override | |
void drive(){ | |
super.drive(); | |
print('sterring towards $destination'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment