Last active
May 31, 2019 14:59
-
-
Save Zenderable/1e874f91888bf9f439fa3e768df3a01c to your computer and use it in GitHub Desktop.
Example of inheritance and polymorphism in Dart
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 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