Skip to content

Instantly share code, notes, and snippets.

@numoonchld
Last active December 29, 2020 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numoonchld/849d68c65dfcc1f2180c7b79141220a6 to your computer and use it in GitHub Desktop.
Save numoonchld/849d68c65dfcc1f2180c7b79141220a6 to your computer and use it in GitHub Desktop.
OOP in Dart - Classes, Inheritence
// 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