Skip to content

Defining a Class

Estimated time to read: 2 minutes

Classes have two aspects that are defined within it:

  • Attributes
  • The attributes are the names of the data the class must contain. The attribute also define the data types (eg, int, flat, char, etc)

  • Methods

  • Methods describe the actions the class must be able to perform. Methods will becomes sections of code but at design time, we only define the method name and a few other components.

Attributes

  • Attributes define the data an object can hold.
  • At design time the actual data type of the attribute can be defined but this is optional.
  • The class file defines all the attributes an object can have, but does not define the actual values. Defaults can be sent, but there is no real need to as the attributes should be set when instantiated.
  • When objects are created from a class, their attributes are the assigned values.
  • Each objects' attributes have their own values. This is what distinguishes one object from another.

Methods

Methods are blocks of code, each of which can be executed. Methods are used to alter the attributes of the object to which they belong.

When designing a system, we do no consider the code inside a method. Instead we define the following, this acts as our interface for the class:

  • Name - The name must not contain spaces
  • Signature - List of values the method accepts for processing; Compromised of the method's name and its parameter list
  • Return value - Single value the method passes back to whatever code invoked it

Usual method types

A mutator is a method that changes the state of an object / its attributes An accessor is a method that exposes the state of the object / its attributes