Sage-Code Laboratory
index<--

Dart Classes

In computer programming we define Classes as custom data types. A class encapsulate data definition and behavior. We use classes in object oriented programming (OOP) as blueprint to create objects. You should already know this theory from Software Engineering tutorials.

Class Anatomy

Next picture show you the features of a Class. As you can see a Class has a name, properties and methods. In this picture we show you an example of a class called: Account with two properties act_number, act_type and 4 methods.

class

Class Anatomy

Pattern:

Next syntax pattern is the most common. A class is by default derived from Object class, you do not have to specify this in class definition. A class can have a constructor that has the same name as the class. Class name start with uppercase so the constructor start also with uppercase.

//define a class
class ClassName {
    //definition of fields (attributes/properties)
    type this.field1;
    ...
    //define eventual setters+getters
    ...
    // define constructor (having same name as the class)
    ClassName(type param1, type param2, ...) {
        this.field1 = param1;
        ...
    }
    // define other functions,
    type functionName(params) {
        ...
    }
}

Practice Example

Next example is here for you to jump ahead into study. The same example is available in Java tutorial. If you are a java developer, you can open:java class in a new window and compare the two examples. If you do not know Java you can just study the example below:

Implement Class

/* define a class */
class Account {
    // class public properties
    int act_number;
    String act_type;
    // class internal field
    int _act_amount = 0;
    //class constructor (same name ass class)
    Account(int p_number, String p_type) {
        this.act_number = p_number;
        this.act_type = p_type;
    }
    //fists class methods
    int withdraw(int p_amount) {
        int result;
        if (_act_amount > p_amount) {
           this._act_amount -= p_amount;
           result = p_amount;
        } else {
           result = p_amount = this._act_amount;
           this._act_amount = 0;
           return result;
        }
    }
    //second method
    void deposit(int p_amount) {
        this._act_amount += p_amount;
    }
    //third method
    int closure() {
        int result = this._act_amount;
        this._act_amount = 0;
        return result;
    }
    //supplementary method
    int get amount {
        return this._act_amount;
    }
} //end of class

Notes:

Test Account


void main() {
    //create an instance of Account
    Account myCheckAccount = new Account(2021,"checking");

    //verify the account number
    print("New account: ${myCheckAccount.act_number}");

    //deposit some money
    print("Deposit 100");
    myCheckAccount.deposit(100);
    print("Account amount is:${myCheckAccount.amount}");

    //withdraw some money
    print("Withdraw 10");
    myCheckAccount.withdraw(10);
    //verify the amount left
    print("Account amount is:${myCheckAccount.amount}");
}

We have used Account class to create a new account object, then deposit 100 and then withdraw 10. The final amount is going to be 90. You can run this example on-line and check the output: dart-account

Homework: In the previous examples, add a new test for closing the account with method: closure(). No, this is not a closure function but a function that close the account. Of course we should have made a _status field but we don't have it. So maybe you can add a new field to. Relax and have some fun messing with the code!

To be continued: There is so much more to talk about classes but for now this is all you need to go ahead with the study. I will continue next with collections, that are actually classes. So you will learn more on the go. Later I may add some details about Classes.


Read next: Collections