create new class with constructor dart code example

Example: constructor in dart

//Creating object.
void main(){
  SelfDrivingCar myLamb = SelfDrivingCar('Floride');
  myLamb.drive();
 
}

//Initializing class Car.
class Car {
  int numberofwheels = 4;
  
  void drive() {
    print('this is a car');
  }
}

//using Inheritance 
class SelfDrivingCar extends Car {
  String destination='k';
  
  SelfDrivingCar(String userDestination){ //constructor
    destination = userDestination;
  }
  @override    //polymorphism.
  void drive() {
    super.drive();  // accessing parent's methods.
    
    print('sterring wheel to $destination');
    
  }
}

Tags:

Misc Example