Skip to content

Instantly share code, notes, and snippets.

@olaide-ekolere
Last active November 27, 2020 12:33
Show Gist options
  • Save olaide-ekolere/61187df4c03d8735d82efda5ee0a3210 to your computer and use it in GitHub Desktop.
Save olaide-ekolere/61187df4c03d8735d82efda5ee0a3210 to your computer and use it in GitHub Desktop.
Inheritance 1 - Object Oriented programming
import "package:flutter/material.dart";
void main() {
//new instance of class
MyNewClass myNewClass = new MyNewClass();
var anotherInstance = new MyNewClass();
print(myNewClass.runtimeType);
anotherInstance.sayHello();
Shape rectangle1 = new Rectangle(20, 30);
print(rectangle1.runtimeType);
print(rectangle1.perimeter());
rectangle1.showPerimeter();
Shape square1 = new Square(35);
print(square1.runtimeType);
print(square1.perimeter());
square1.showPerimeter();
Rectangle(20, 20).showPerimeter();
Square(20).showPerimeter();
Shape circle1 = Circle(radius: 66);
circle1.showPerimeter();
Shape circle = Circle.fromDiameter(56);
circle.showPerimeter();
}
class MyNewClass {
void sayHello() {
print("hello");
}
}
class Rectangle extends Shape {
Rectangle(int length, int breadth) : super(length, breadth);
}
class Square extends Shape{
Square(int size) : super(size, size);
}
class Circle extends Shape{
Circle({@required int radius}) : super(radius, radius);
Circle.fromDiameter(int diameter) : assert(diameter%2==0, "Must be divisible by 2"), super(diameter~/2, diameter~/2);
@override int perimeter() {
return (2 * length * 3.14).toInt();
}
}
class Shape {
int length;
int breadth;
Shape(this.length, this.breadth);
int perimeter() {
return 2 * (length + breadth);
}
void showPerimeter() {
print("Perimeter is ${perimeter()}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment