Skip to content

Instantly share code, notes, and snippets.

@Zenderable
Last active May 31, 2019 14:59
Show Gist options
  • Save Zenderable/1e874f91888bf9f439fa3e768df3a01c to your computer and use it in GitHub Desktop.
Save Zenderable/1e874f91888bf9f439fa3e768df3a01c to your computer and use it in GitHub Desktop.
Example of inheritance and polymorphism in Dart
void main() {
Car nissan = Car();
ElectricCar opel = ElectricCar();
FlyingCar vw = FlyingCar();
print(nissan.numberOfSeats);
nissan.drive();
print(opel.numberOfSeats);
opel.drive();
vw.drive();
}
class Car {
int numberOfSeats = 5; //properties
void drive() {
print('It\'s driving with LPG');
}
}
class ElectricCar extends Car {
@override //it override drive method from car class
void drive() {
print('It\'s driving with electricity');
}
}
class FlyingCar extends ElectricCar {
@override
void drive() {
super.drive(); //parent method
print('Wow! It\'s flying!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment