Skip to content

Instantly share code, notes, and snippets.

@narenarjun
Created February 19, 2020 07:15
Show Gist options
  • Save narenarjun/c632771ddb91e81e35e977b0f769a889 to your computer and use it in GitHub Desktop.
Save narenarjun/c632771ddb91e81e35e977b0f769a889 to your computer and use it in GitHub Desktop.
explains about abstract classes,interfaces, mixins and castings in dart
import 'dart:math';
void main() {
A c = C("tokyo");
c.hello();
(c as B).goodBye();
(c as C).hi();
(c as C).printStamp();
}
class TimeStamp {
DateTime time = DateTime.now();
void printStamp() {
print(time);
}
}
/// mixins has to follow certain conditions
// 1) the class must have no declared constructors
// 2) the class must not be a subclass of anything other than String() [cannot use the extends keyword ]
// 3) it must have a no calls to the super keyword, [in other words it must not inherit anything]
// void main() {
// // Square square = Square(23.0);
// // Rectangle rectangle = Rectangle(12.0, 30.0);
// // Circle circle = Circle(15.0);
// // print(square.name);
// // print(rectangle.name);
// // print(circle.name);
// Shape randShape;
// Random random = Random();
// int choice = random.nextInt(3);
// switch (choice) {
// case 0:
// randShape = Circle(
// random.nextInt(10) + 1.0,
// );
// break;
// case 1:
// randShape = Rectangle(
// random.nextInt(10) + 1.0,
// random.nextInt(10) + 1.0,
// );
// break;
// case 2:
// randShape = Square(
// random.nextInt(10) + 1.0,
// );
// break;
// default:
// print("will never exceute");
// }
// print(randShape.name);
// print(randShape.area);
// print(randShape.perimeter);
// }
class A {
void hello() {
print("hello from A");
}
}
class B {
String b;
B(this.b);
void hi() {
print("hi from B");
}
void goodBye() {
print("bye bye $b");
}
}
class C with TimeStamp implements A, B {
C(this.b);
@override
void hello() {
print("hello from C");
}
@override
void hi() {
print("hi from C");
}
@override
String b;
@override
void goodBye() {
print("this is overrided print from $b");
}
}
abstract class Shape {
double get perimeter;
double get area;
String get name;
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double get area => pi * (radius * radius);
@override
double get perimeter => 2 * pi * radius;
@override
String get name => "I'm a circle with a radius $radius";
}
class Rectangle extends Shape {
double length, width;
Rectangle(this.length, this.width);
@override
double get area => length * width;
@override
double get perimeter => 2 * (length + width);
@override
String get name =>
"I'm a reactangle with a length: $length and a height: $width";
}
class Square extends Rectangle {
Square(double side) : super(side, side);
@override
String get name => "I'm a Square with a side of $length";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment