teach-ict.com logo

THE education site for computer science and ICT

3. Class parts

The parts that make up a class is shown below

pseudocode

A pseudocode class begins with the word 'class' and ends with 'endclass'.

It is also given a name so we can refer to it, in this case the class is called 'Car'.

It is important to note that a class is a effectivley a software 'template'. The structure is used to create objects in memory, which is discussed in a later page.

Attributes

The Car class has a number of attributes that can be altered.

In the above example, the attributes are "colour", "model", "brand", "speed" and "direction".

If the value of an attribute can be altered, then it is stored as a variable. If you don't want to allow the value to be altered, then it is stored as a constant. Attributes are normally 'private' which means only methods within the class can alter their value. It is possible to have 'public' attributes i.e. variables that can be altered directly by external code, but that kind of loses the point of object orientated programming.

Constructor

A class can have many methods i.e. functions, that use the methods' attributes.

The most important of these methods is the constructor. This is the method that creates an instance of the class i.e. it creates an object. When an object is to be created, the contructor is called.

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What is a OOP class