Created
May 2, 2022 08:00
-
-
Save AbdullohBahromjonov/a6c21e538be75d504b1c691066686df3 to your computer and use it in GitHub Desktop.
Dart "extends" and "implements" interfaces
This file contains hidden or 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
abstract class Person { | |
String get name; | |
int get age; | |
void speak(String name, int age); | |
void walk() { | |
print('walk'); | |
} | |
} | |
class Male extends Person { | |
final String name; | |
final int age; | |
Male(this.name, this.age); | |
@override | |
void speak(String name, int age) { | |
final voice; | |
if (age >= 15) { | |
voice = 'man'; | |
} else { | |
voice = 'boy'; | |
} | |
print('$name speaks with $voice voice'); | |
} | |
} | |
class Female implements Person { | |
final String name; | |
final int age; | |
Female(this.name, this.age); | |
@override | |
void speak(String name, int age) { | |
final voice; | |
if (age >= 18) { | |
voice = 'woman'; | |
} else { | |
voice = 'girl'; | |
} | |
print('$name speaks like a $voice'); | |
} | |
@override | |
void walk() { | |
print('walks slow'); | |
} | |
} | |
void main() { | |
final abdulloh = Male('Abdulloh', 15); | |
final kamola = Female('Kamola', 17); | |
print(abdulloh.name); | |
print(abdulloh.age); | |
abdulloh.speak(abdulloh.name, abdulloh.age); | |
print(kamola.name); | |
print(kamola.age); | |
kamola.speak(kamola.name, kamola.age); | |
kamola.walk(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment