Friday 4 May 2012

Interfaces & Casting

Interfaces extend the scope of a class by adding their own components to its public section. This allows users to address different classes via a universal point of contact. Interfaces along with Inheritance provide one of the pillars of polymorphism, since  they allow a single method within an interface to behave differently in different classes.

Interface components do not have to be assigned individually to a Visibility section, Since they automatically belong to the public section of the class in which the interface is Implemented.

Interfaces do not have Implementation
Interfaces do not have Instances.

Interface References

Reference variables allow you to access object. Instead of creating reference variables with reference to a class you can also define them with reference to an interface. This kind of reference variable can contain references to objects of classes that implement the corresponding Interface.

Addressing Objects Using Interface References

To create an object of the class say C1, You must first have declared a reference variable say CREF.
(i.e. Data : CREF type ref to C1).)
Suppose if the class 'C1' implements an interface 'I1'.
Interface Reference here is IREF.
(data : IREF type ref to I1).

Then the following assignment is possible between CREF & IREF.

IREF = CREF.

Assignment using interface references- Casting

Like class references, you can assign interface references to different reference variables( be it a class reference or interface reference)

When you use the MOVE statement or the assignment operator (=) to assign the reference variables, the system must be able to recognize  the syntax check whether an assignment is possible.

Suppose we have a class reference CREF and Interface reference IREF, IREF1 & IREF2

The following assignments with interface references can be checked statically

Case1
Both interfaces(iref1 and iref2) must refer to the same Interface(iref) or the interface IREF1 must contain the interface IREF2as a component. The following assignment is possible

IREF1 = IREF2.


Case2
The class of the class reference CREF must implement the interface of the interface reference IREF

IREF = CREF.


Case3
The class of CREF must be the predefined empty class OBJECT.

CREF = IREF.

In all other cases you would have to work with the statement  Move....?To  or the Casting Operator (?=).

The casting operator replaces the assignment operator (=) in the MOVE....?TO statement .
When you use the casting operator there is no Static type check. Instead the system checks @ Runtime whether the object reference in the source variable points to an object to which the object reference in the target variable can also point. If the assignment is possible the system makes it, otherwise the catchable runtime error  MOVE_CAST_ERROR occurs.
You must always use casting for assigning an interface reference to a class reference if CREF does not  refer to the predefined empty class OBJECT

CREF ?= IREF.

For Casting to be successful, the object to which IREF points, must be an object of the same class as the type of the class variable CREF.
(i.e Data : iref type ref to i1,
Here 'i1' is the interface being implemented by class 'c1'.
Data cref type ref to c1.)
Here IREF & CREF points to the object of the same class ''c1".
Hence the above Casting is Successful. :-)





No comments:

Post a Comment