Classes and Objects

Getters Setters

// provide read and write access to an object’s properties
class Cat {
    String name;

    // getter
    String get catName {
        return name;
    }
    // setter
    void set catName(String name){
        this.name = name;
    }
}

Abstract Classes

// abstract class—a class that can’t be instantiated
// This class is declared abstract and thus can't be instantiated.
abstract class AbstractContainer {
  // Define constructors, fields, methods...
  void updateChildren(); // Abstract method.
}

Constructors

class Cat {
    String name;
    Cat(this.name);
}
void main(){
    Cat myCat = Cat("Kitty");
    print(myCat.name); // Prints: Kitty
}

Object

// instance of a class
// below myCat is Object of class Cat
void main(){
    Cat myCat = Cat();
    myCat.name = "Kitty";
    myCat.voice(); // Prints: Meow
}

Class

class Cat {
    String name;
    // method
    void voice(){
        print("Meow");
    }
}
Comments