Skip to content

Instantly share code, notes, and snippets.

@mhmadip
Created October 15, 2021 17:23
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 mhmadip/c1cc4d73575c8603bcc16bc03fd5428a to your computer and use it in GitHub Desktop.
Save mhmadip/c1cc4d73575c8603bcc16bc03fd5428a to your computer and use it in GitHub Desktop.
Explaining OOP in Dart
// classes and objects example
/*
class Car{
int numberOfDoors=5;
void drive(){
print('wheels start turning');
}
}
void main(){
var myCar= Car();
myCar.drive();
myCar.numberOfDoors=4;
print(myCar.numberOfDoors);
}*/
//Abstraction in Action
/*class Square{
int? side;
Square({this.side});
int getPerimeter(){
return side!*4;
}
}
void main(){
Square mySquare= Square(side: 10);
print(mySquare.getPerimeter());
} */
//Encapsulation in Action
/*
class Human{
int? _superSecretVariableForTheAge;
Human();
void setAge(int newAge){
this._superSecretVariableForTheAge=newAge;
}
int get getAge{
return _superSecretVariableForTheAge!;
}
}
void main(){
Human mySlef= Human();
mySlef.setAge(33);
print(mySlef.getAge);
mySlef._superSecretVariableForTheAge=33;
}
*/
//Inheritance
class Car{
int numberOfSeats=4;
void drive(){
print('wheels turn.');
}
}
class ElectricCar extends Car{
int batteryLevel=100;
void recharge(){
batteryLevel=100;
}
}
class SelfDrivingCar extends Car{
String? destination;
SelfDrivingCar(String userSetDestination){
destination=userSetDestination;
}
@override
void drive() {
// TODO: implement drive
super.drive();
print('steering towards $destination');
}
}
class LevitatingCAr extends Car{
@override
void drive() {
// TODO: implement drive
print('glide forwards');
}
}
void main() {
Car myNormalCar = Car();
print(myNormalCar.numberOfSeats);
myNormalCar.drive();
ElectricCar myTesla= ElectricCar();
myTesla.drive();
SelfDrivingCar myWaymo = SelfDrivingCar("Nishtiman Bazzar");
//polymorphism
myWaymo.drive();
LevitatingCAr myAeroMobil = LevitatingCAr();
myAeroMobil.drive();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment