Last active
November 28, 2020 09:05
-
-
Save olaide-ekolere/f4db8f2cf9e62a25cc4fc5fdb264ee7f to your computer and use it in GitHub Desktop.
Inheritance (Abstract Class & Getters/Setters) - Object Oriented programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
Circle circle1 = Circle(radius: 66); | |
circle1.showPerimeter(); | |
print(circle1.diameter); | |
circle1.showDiameter(); | |
circle1.diameter = 220; | |
circle1.showDiameter(); | |
circle1.showPerimeter(); | |
//Shape shape = new Shape(1, 2); | |
} | |
class MyNewClass { | |
void sayHello() { | |
print("hello"); | |
} | |
} | |
class Rectangle extends Shape { | |
Rectangle(int length, int breadth) : super(length, breadth); | |
void showPerimeter() { | |
print("The perimeter of rectangle with length $length and breadth $breadth is ${perimeter()}"); | |
} | |
} | |
class Square extends Shape{ | |
Square(int size) : super(size, size); | |
void showPerimeter() { | |
print("The perimeter of square with size $length is ${perimeter()}"); | |
} | |
} | |
class Circle extends Shape{ | |
Circle({@required int radius}) : super(radius, radius); | |
@override int perimeter() { | |
return (2 * length * 3.14).toInt(); | |
} | |
int get diameter { | |
return length * 2; | |
} | |
set diameter(int d){ | |
length = d ~/ 2; | |
} | |
void showDiameter() { | |
print("the diameter of the circle is $diameter"); | |
} | |
void showPerimeter() { | |
print("The perimeter of circle with radius $length is ${perimeter()}"); | |
} | |
} | |
abstract class Shape { | |
int length; | |
int breadth; | |
Shape(this.length, this.breadth); | |
int perimeter() { | |
return 2 * (length + breadth); | |
} | |
void showPerimeter(); | |
//add area | |
} | |
//add 3DShape | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment