Skip to content

Instantly share code, notes, and snippets.

@muchirijane
Created September 19, 2019 10:45
Show Gist options
  • Save muchirijane/77d0f2dafa67ea2d0aae3e8c1eaf5ffa to your computer and use it in GitHub Desktop.
Save muchirijane/77d0f2dafa67ea2d0aae3e8c1eaf5ffa to your computer and use it in GitHub Desktop.
// INHERITANCE AND POLYMORPHISM
void main(){
//Car myCar = Car();
// myCar.drive();
// print (myCar.numberOfSeats);
// ElectricCar myNewCar = ElectricCar ();
// print(myNewCar.numberOfSeats);
LevitatingCar myLimo = LevitatingCar();
myLimo.drive();
SelfDrivingCar myTesla = SelfDrivingCar('Going towards Thika Road');
myTesla.drive();
}
class Car {
int numberOfSeats = 5;
void drive (){
print('turn wheels');
}
}
// The electric car class can inherite the properties
//of the car class by using extends
class ElectricCar extends Car {
int batteryLevel = 100;
void recharge (){
batteryLevel = 100;
}
}
class LevitatingCar extends Car {
@override
void drive (){
print('grinde forward');
}
}
class SelfDrivingCar extends Car {
String destination;
SelfDrivingCar ( String userDestination){
destination = userDestination;
@override
void drive (){
super.drive(); // calling the function of the Car parent class
print('sterring towards $userDestination');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment