To instantiate a subclass, you declare a reference variable with the type of the relevant class. Then you use the
NEW operator to create the instance. You must pass values to all obligatory parameters of the constructor.
Using Superclass References
A reference variable with the type of a superclass (in this case lcl_plane) can also hold references to objects with the type of a subclass. This makes sense, since a passenger plane or a cargo plane is a plane.
However, a reference variable with the type of the superclass is only aware of the components defined in the superclass. Consequently, when you use a reference with this type to manage an instance of a subclass, you can only address the components from the superclass, even though the actual object to which the reference points is an instance of the subclass.
Assigning object references to a reference variable of a different type is called casting. Assigning a reference that points to a subclass to a reference variable with the type of a superclass is called an up-cast, because you are making the assignment to a type higher up in the inheritance hierarchy.
Calling Methods After an Up-Cast
When you use a reference variable with type ref to lcl_plane to manage a passenger plane instance, you cannot use it to access components that belong to the passenger plane class, as the plane class does not know that they exist and the syntax check cannot know what type of object the reference will actually point to at runtime.
Using the reference, you can, however, access methods whose original definition is contained in the class. This is the case with the get_attributes method in the example in the figure. The question here is which implementation of the method will be called. In this case, the system looks at runtime to see what type of object the reference variable is pointing to, and calls the implementation from the corresponding class. In other words, although we are using a plane reference to manage an instance of the passenger plane class, it is still the method implementation from the passenger plane class that is called.
Assigning a Generic Reference to a Specific Type
You can always assign a reference variable with the type of a subclass to a reference variable with the type of a superclass. For example, you can always assign a passenger plane reference to a plane reference. As we have seen, this is because a passenger plane is a plane. Assigning reference variables in the opposite direction, however, is more tricky. This is because a plane reference can contain references to other classes than the passenger plane class. In other words, where every passenger plane is a plane, not every plane is a passenger plane.
Given an object references plane with type ref to lcl_plane and passenger_plane with type ref to lcl_passenger_plane, the system will always allow you to write plane = passenger_plane. However, the assignment passenger_plane = plane leads to a syntax error because the syntax check cannot be sufficiently sure that the plane reference will actually hold a reference to a passenger plane object at that moment.
The CAST Operator
The syntax check does not let you assign a superclass reference directly to a subclass reference. However, you can still do it using the CAST operator. Casting means looking at an object as though it has a different type. In the example, we tell the system to treat "plane" as though it were an instance of the passenger plane class. This is syntactically correct, as we are now pretending that "vehicle" on the right-hand side of the expression has the same type as "passenger" on the left-hand side. However, there is still no guarantee that plane actually contains a passenger plane reference, and if it turns out not to contain one at runtime, the assignment cannot be made. Left unprotected, this assignment could cause a runtime error.
Securing a Down-Cast
If you are not certain that the object to which a reference variable is pointing is actually an instance of the correct subclass, you can check it using the logical expression IS INSTANCE OF. In this example, the assignment to the passenger plane reference is only processed if plane actually points to an instance of the class lcl_passenger_plane.