Skip to content

Instantly share code, notes, and snippets.

@kharioki
Last active April 20, 2020 22:37
Show Gist options
  • Save kharioki/c094f58e10b1d95da57ef69ff7eea245 to your computer and use it in GitHub Desktop.
Save kharioki/c094f58e10b1d95da57ef69ff7eea245 to your computer and use it in GitHub Desktop.
Inheritance of Action
// Inheritance of Actions
void main() {
// Car myNormalCar = Car();
// myNormalCar.drive();
// print(myNormalCar.numberOfSeats);
ElectricCar myTesla = ElectricCar();
myTesla.drive();
// using polymorphism
SelfDrivingCar myWaymo = SelfDrivingCar('Waiyaki way');
myWaymo.drive();
}
class Car {
int numberOfSeats = 5;
void drive() {
print('Wheels turn');
}
}
class ElectricCar extends Car {
int batteryLevel = 100;
void recharge() {
batteryLevel = 100;
}
}
class LevitatingCar extends Car {
// polymorphism
// override the drive function
@override
void drive() {
print('glide forwards');
}
}
class SelfDrivingCar extends Car {
// create a new propperty set when you create a new car
String destination;
SelfDrivingCar(String userSetDestination){
destination = userSetDestination;
}
@override
void drive() {
super.drive();
print('steering towards $destination');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment