Interview Questions

What is class in Delphi?

Delphi Interview Questions


(Continued from previous question...)

What is class in Delphi?

The Class keyword is the central part of Object Oriented code. It starts the definition of literally a 'class' of object types.

This definition contains so-called 'members' - data and methods (subroutines). When an object of the class is created, it is a stand alone item - you can access the data and methods of the object independently of any other object. It is like a Delphi record, but with active components - methods.

These elements are defined in the class type definition. The method elements are implemented in the implementation section of the Unit.

A class declaration has this typical basic layout :

type
className = class(BaseClass)
private
// Data/method defs local to this Unit
protected
// Data/method defs local to this class + descendants
public
// Data/method defs usable with all objects of this class
published
// Externally interrogatable public definitions
end;


There are a number of uses of the word Class:

1. A class definition based on the TObject class by default. All classes must be based on another class, with TObject being the default, highest level class. TObject provides rudimentry functionality - you would normally define all of the data and methods yourself, and use none from TObject.
Within the definitions of a Class, you can prefix function or procedure definitions with the Class keyword. This allows the subroutine to be called from the Class itself in addition to an object instance of the class. Because a class is not a real object, it has no storage allocated to data. So a Class subroutine must operate without reference to internal class data.

2. Class definition based on a specified class but with no local additions or changes. You have a new class that acts identically to the BaseClass

3. Forward class declaration. It allows all classes in a Unit to be listed at the start of the type section. So this is a matter of convenience rather than anything else.

4. Class definition based on a specified class. As 1 above, but you can specify which class you are basing your new class on. Your class will again be a mixture of ancestor and local declarations.

5. A class can contain implementations of externally predefined interfaces.

6. A metaclass reference allows a variable to be used to reference a class rather than an object.

(Continued on next question...)

Other Interview Questions