Skip to content

SpecificationIX · Objects

9.1

Classes and attributes

A class is a mould: it describes what data every object keeps (its attributes) and what it can do (its methods).

Markdown version

9.1.1 How to write it

Class «Nombre» ⟦Inherits From «Superclase»⟧
  ⟦Public | Private⟧ Define «atributo» As «tipo»;
  «métodos y constructor»
EndClass

9.1.2 How it works

A class is a sibling unit of the algorithm: it does not go inside it or inside another class. Its name is a new type, and every value of that type is an object created with [New](#objetos.constructor).

ProgramA class with two attributes

Class Punto
  Define x As Integer;
  Define yy As Integer;
EndClass

Algorithm Puntos
  Define p As Punto <- New Punto();
  p.x <- 3;
  p.yy <- 4;
  Write p.x, ",", p.yy;
EndAlgorithm

Output

3,4

The program finishes normally.

Figure 9.1-1.A class and two objects created from it. Each object has its own attributes.

At the top, the class Punto with the attributes x and yy. Two arrows marked New lead to two objects: one with x = 3 and yy = 4, the other with x = 0 and yy = 7.

Attributes start without a value: they accept no initializer. A Private attribute can only be used from the code of its own class.

Class-typed variables hold a reference to the object, not the object: assigning or passing by value copies the reference and both variables name the same object. Equality = between objects compares identity. There is no null value you can write.

In short

  • A class declares attributes and methods; its name is a type.
  • Attributes are declared with Define and start without a value.
  • Private limits access to the class's code.
  • Variables hold references; = compares identity.