Dart Inheritance : Type Interface in a Mixed List

Dart Inheritance : Type Interface in a Mixed List

·

1 min read

Type interface in a Mixed List :

  • ```dart abstract class Shape { double get area; }

    class Circle extends Shape { double radius;

    Circle(this.radius);

    @override double get area => 3.14 radius radius;

    @override String toString() => 'Circle with radius $radius';

    }

    class Rectabgle extends Shape { double width; double height;

    Rectangle(this.width,this.height);

    @override double get area => width * height;

    @override String toString() => 'Rectangle with width $width and height $height';

}

void main() { List shapes =[ Circle(5.0), Rectangle(3.o,4.0), Circle(2.0); ];

for (var shape in shapes) { print('Shape: $shape'); print('Area: ${shape.area}'); if(shape is Circle) { print('It\'s a circle!.'); } else if (shape is Rectangle) { print('It\'s a rectangle!.'); } print('---------------'); } }



* ```dart
    Shape: Circle with radius 5.0
    Area : 78.5
    It's a Circle!.
    ------------

    Shape: Rectangle with widht 3.0 and height 4.0
    Area: 12.0
    It's a rectangle!
    -------------

    Shape: Circle with radius 2.0
    Area: 12,56
    It's a circle!.
    ------------

    Exited.