Classes

  • Dart is an object-oriented language with classes and mixin-based inheritance.
  • Every object is an instance of a class, and all classes descend from Object.
  • Mixin-based inheritance means that although every class (except for Object) has exactly one superclass, a class body can be reused in multiple class hierarchies.

Using class members

Use a dot (.) to refer to an instance variable or method.

Use ?. instead of . to avoid an exception when the leftmost operand is null.

Getting an object’s type

To get an object’s type at runtime, you can use Object’s runtimeType property, which returns a Type object.

Extending a class

class Television { 
   void turnOn() { 
      _illuminateDisplay(); 
      _activateIrSensor(); 
   } 
   // ··· 
} 

class SmartTelevision extends Television { 
   void turnOn() { 
      super.turnOn(); 
      _bootNetworkInterface(); 
      _initializeMemory(); 
      _upgradeApps(); 
   } 
   // ··· 
}

Use extends to create a subclass, and super to refer to the superclass.

Overriding members

class SmartTelevision extends Television { 
   @override void turnOn() {...} 
   // ··· 
}

Subclasses can override instance methods, getters, and setters.

Classes — Structure map

Clickable & Draggable!

Classes — Related pages: