The three visibility areas (public section, protected
section, private section) are the basis for one of the important features of
object orientation, Encapsulation. You should take great care in designing the
public components and try to declare as few public components as possible when
you define a class. Once you have released the class, the public components of
global classes may not be changed.
Public attributes are visible externally and form a part of
the interface between an object and its users. If you want to encapsulate the
state of an object fully, then you cannot declare any public attributes. Apart
from defining the visibility of an attribute, you can also protect it from
changes using the READ-ONLY addition.
Inheritance allows you to derive a new class from an
existing class. It is done by using the INHERITING FROM addition in the
CLASS <subclass> DEFINITION INHERITING FROM <superclass>
All of the components of the existing class
<superclass> are inherited by the new class <subclass>. The new
class is called the subclass of the class from which it is derived. The
original class is called the superclass of the new class. It contains the same
components as the superclass if you do not add any new declarations to the
subclass. However, in the subclass only the public and protected components of
the superclass are visible. The private components of the superclass are not
visible though they exist in the subclass. You can declare private components
in a subclass that have the same names as private components of the superclass.
It is seen that each class works with its own private components. The another
point that we note is that methods which a subclass inherits from a superclass
use the private attributes of the superclass and not any private components of
the subclass with the same names.
The subclass is an exact replica of the superclass if the
superclass does not have any private visibility section. However, you can add a
new component to the subclass because it allows you to turn the subclass into a
specialized version of the superclass. If a subclass is itself the superclass
of further classes, then you can introduce a new level of specialization.
Reference variables are defined with reference to a
superclass or an interface defined with reference to it can also contain
references to any of its subclasses. A reference variable defined with
reference to a superclass or an interface implemented by a superclass can
contain references to instances of any of its subclasses, since subclasses
contain all of the components of all of their superclasses and also convey that
the interfaces of methods cannot be changed. In particular, you can define the
target variable with reference to the generic class OBJECT.
Using the CREATE OBJECT statement, when you create an object
and a reference variable typed with reference to a subclass then you can use
the TY PE addition to which the reference in the reference variable will point.
A reference variable can be used by a static user to address
the components visible to it in the superclass to which the reference variable
refers. However, any specialization implemented in the subclass cannot be
addressed by it.
Depending on the position in the inheritance tree at which
the referenced object occurs, you can use a single reference variable to call
different implementations of the method. This is possible only if you redefine
an instance method in one or more subclasses. This concept is called
polymorphism, in which different classes can have the same interface and,
therefore, be addressed using reference variables with a single type.