Last active
October 11, 2019 23:37
-
-
Save kirkdrichardson/847473dc3db9ba5ad9848ecf7b00ba7e to your computer and use it in GitHub Desktop.
Abstract classes and methods in Dart
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 classes and methods in Dart (as of v2.5) | |
// See live at https://dartpad.dartlang.org/847473dc3db9ba5ad9848ecf7b00ba7e | |
void main() { | |
// Expect Error! - Can't instantiate abstract classes | |
// final Person p = Person(); | |
final Person sartre = Philosopher(); | |
print('${sartre.name} of ${sartre.residence} says, "${sartre.sayCatchPhrase()}"'); | |
sartre.sayCatchPhrase(); | |
sartre.sleep(); | |
} | |
abstract class Person { | |
String name; | |
String residence; | |
String sayCatchPhrase(); | |
// Default methods are allowed in abstract classes | |
void sleep() { | |
print('zzzzz....zzzz...zz...zzzzz.....zzzz'); | |
} | |
} | |
class Philosopher extends Person { | |
String name = 'Sartre'; | |
String residence = 'France'; | |
// Expect Error! - Abstract methods can only exist inside abstract classes. | |
// void abstractMethodInConcreteClass(); | |
// Must override abstract methods in first implementing concrete class. | |
String sayCatchPhrase() { | |
return 'Hell is other people'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment