Skip to content

Summary

Classes are a tool for structuring code into entities that contain data and methods.

Each class definition is a blueprint that describes an entity in terms of what it knows (data) and what it can do (methods).

A class defines a custom data type we can use in our programs when building digital realities.

Classes have a name, and contain zero or more members.

  • A class member can be a:
    • field - to store data.
    • constructor - to initalise objects when they are created.
    • method - to perform an action.
    • property - to give access to data.

Each class member can be public or private:

  • public members can be accessed by any code that has an instance of the class.
  • private members can only be accessed by other code in the same class.

Fields are just like variables, with a scope of the class in which they are declared.

Methods capture the actions that an object can perform. This is similar to functions and procedures in procedural programming.

Constructors are special methods which create and return an instance of the class in which they are declared.

  • A default constructor is a constructor with no parameters.

Properties allow us to write code to access data from within an object.

Classes can be used to create objects.

At runtime, objects perform the actions needed to produce the programs desired behaviour.