Skip to content

Instantly share code, notes, and snippets.

@angwandi
Created February 8, 2020 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angwandi/3fffc9fe8d970f3272c8ebc72b9dc80e to your computer and use it in GitHub Desktop.
Save angwandi/3fffc9fe8d970f3272c8ebc72b9dc80e to your computer and use it in GitHub Desktop.
polymorphism : different forms
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