Last active
December 29, 2020 00:17
-
-
Save numoonchld/849d68c65dfcc1f2180c7b79141220a6 to your computer and use it in GitHub Desktop.
OOP in Dart - Classes, Inheritence
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
// Define main function | |
void main() { | |
// 04. CLASSES ============================ | |
print('\n04. CLASSES: \n------------------------------'); | |
User userOne = User('ancilla',30); | |
print(userOne.username); | |
print(userOne.age); | |
User userTwo = User('victor',26); | |
print(userTwo.username); | |
print(userTwo.age); | |
userOne.login(); | |
userTwo.login(); | |
// super class (child class) - Inheritance | |
SuperUser userThree = SuperUser('leest', 35); | |
print(userThree.username); | |
print(userThree.age); | |
userThree.login(); | |
userThree.publish(); | |
// throws Error: Compilation failed. | |
// userTwo.publish(); | |
} | |
// Define a Class outside the main function | |
class User { | |
String username; | |
int age; | |
User(String username, int age) { | |
this.username = username; | |
this.age = age; | |
} | |
void login() { | |
print( this.username + ' has logged in!'); | |
} | |
} | |
// Define a super (child) class - SuperUser | |
class SuperUser extends User { | |
SuperUser(String username, int age): super(username, age); | |
void publish() { | |
print( this.username + ' has published!' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment