The Dart programming language uses classes and interfaces similarly to Java, C# and PHP. Dart supports single inheritance similarly to Java, C# and PHP. Dart supports multiple interfaces implementation similarly to Java, C# and PHP. As in Java and in C# every class inherits (by default) from the Object class. The classes can have public and private members. The public accessibility is the default one. Dart supports a special shorthand syntax for accessing class properties, similarly to properties in C#.

class Rectangle
{
  double _width;
  double _height;
  Rectangle(double w,double h)
  {
    width = w;
    height = h;
  }
  get width => _width;
  set width(size) => (size>0)?_width=size:_width=10.0;
  get height => _height;
  set height(size) => (size>0)?_height=size:_height=10.0;
  area() => width * height;
  perimeter() => 2*(width+height);
}

main()
{
  var ob = new Rectangle(3.2,4.4);
  ob.width = 20.2;
  ob.height = -4.1;
  var area = ob.area();
  print("area is $area");
}

The following video clip overviews this code sample, shows its execution and explains it.

Leave a Reply

Your email address will not be published. Required fields are marked *