Understanding Down Cast Assignment in Object Oriented Programming

Example to Explain the Down Cast Assignment in OOPs ABAP

OFTEN NOTICE ‘?=’ KEYWORD IN SAP. HAVE YOU EVER WONDERED WHY? THE ANSWER LIES HERE!!

Down Cast means assigning the reference of a Super Class back to a SubClass. Those objects which refer to the Super Class can also refer to the SubClass reference at the runtime.

Why are the Sub Classes Wide?

Ans – The subclasses which inherit the parent class would usually have MORE (Wide) components (attributes, methods etc) than the parent class. In real use cases, we inherit the parent class to child class and then add extra features to the child class. Hence Sub Classes are Wide.

Opposite of Wide is Narrow. Since Parent Classes have FEW(Narrow) components (usually) compared to Child Classes, there for Parent Classes are Narrow.

Why are the Sub Classes Down?

Ans – Look at the above image. The Parent Class is on Top and Sub Classes are below it in the hierarchy. That’s why Sub Classes are Down.

The above two explanations might not be technically 100% true, but it helps in understanding our concept.

To sum up, when we assign the reference of Parent Class to Child, it is termed as Widening Cast or Down Casting. When Child Class reference is assigned to Parent Class, it is termed as Narrowing Cast or Up Casting.

Personally, I like it to be referred as Narrow Casting and Wide Casting. We would use Narrowing Cast and Narrow Casting interchangeably in this article. Also Widening Cast and Wide Casting.

Real Time Scenario: A user who wants the finest data of the subclass, he can copy the reference of super class back to the subclass and extract details provided in the subclass only.

Example :

There is a superclass Person which has a method display_name.

There is a subclass Employee which is inhertited from super class Person and has method display_name inherited and its own method display_designation.

The super class reference is assigned back to the sub class reference by using the down cast assignment operator MOVE TO or ” ?= “. After this assignment, the user is no longer limited to the inherited methods. In the given example all components of the Class employee can also be accessed after the down cast assignment along with the inherited methods of the superclass. This is explained via an example which also diffrentiates between upcast and downcast.

Step 1 . Go to se38 and create a report . Create a Super class person as follows:

Step 2. Write down the implementation of the class Person.

Step 3. Create a class Employee inheriting from Super class Person.

Step 4. Create the implementation of the class Employee.

Step 5. Create another class Student inheriting from Super class Person.

Step 6. Create its implementation in which only super class’s method display_name is redefined.

Step 7.1 Assign the Reference Type to the Classes.

The reference type is the type of the pointer (in this case r_person, r_employee and r_student) to the memory space. It’s the gateway or the “API” through which we can access the memory of the objects person, employee, and student respectively.

Step 7.2 Bring the Object Type to Life.

Assume the Objects (person, employee, and student) as memory space that contains information (viz attributes, methods etc) and whose TYPE never changes after it is instantiated.

Case 1 – No Casting (Straight Display)

Call the common method of the Parent (r_person) and Child (r_employee, r_student) Objects.

As expected, there is no surprise. Methods of the respective Objects are triggered.

Case 2 – Narrow Cast or Up Cast i.e. Assign Child Class Reference to Parent Class

r_person = r_student.

If you look closely, we are using CALL METHOD r_person->display_name syntax. But the display_method of Child Class Student is being triggered and not the display_method of Parent Class Person.

DOES IT RING SOMETHING?

This is called Up Casting or Narrow Casting where Parent refer to the Child Class Objects.

We can also use syntax r_person ?= r_student. “?=” is syntactically and technically correct. But SAP does not recommend it.

TRIVIA 1

Some Technical folks debate on the use of “?=” in favor of ONLY in case of Widening Cast or DownCast. So that it leads to consistency in code and if anyone finds ‘?=”, it should be only for Widening Cast.

But others debate that “?=” should be used whenever the Left-Hand Side and Right-Hand Side Types are different. They say, it will help the developers to know that, LFS and RHS check would happen only at Runtime.

Hope by now, you know, “?=” is called the Casting Operator which instructs the compiler to overlook the LHS and RHS types during Compilation. When we use the Casting Operator “?=”, the LHS and RHS check happens during Runtime.

Case 3 – Another way to Narrow/Up Cast.

Please check, originally ls_person and lt_person are declared as Reference to Person (Parent Class).

But at Runtime, the child class objects can be assigned to parent class. This is Narrow Casting or Up Casting.

In other words, UpCasting where the references are appended to the table which holds the superclass reference.

In the loop, when method display_name is called, the super class reference which holds the child class reference will now call the child class methods.

Assign the reference of the sub class in the super class reference.

Case 4 – Widening Cast or Down Cast i.e. Assign Parent Class Reference to Child Class

Now, the reference to the superclass ls_person contains the rerefence of the child class that is Employee class in ls_person (as employee object was the 2nd row in the loop). We will again assign the super class reference back to the sub class reference which is known as Down Cast to access methods of the sub class Employee.

This code is written in TRY ENDTRY block because during Down Casting (Widening) , the runtime system checks before assignment whether the source variable corresponds to the type requirements of the target variable and gives the error message in case of something wrong (does not dump).

TRIVIA 2

What would happen if we Down Cast (Widening Cast) Parent to Student class rather than Parent to Employee class for the current code in our example?

If the variable ls_person would have contained the reference of sub class Student and then this type assignment of assigning to the refernce of sub class employee would have been done, then it would have caused a runtime error. Let’s TRY IT OUT !!!!

Output

Why?

Ans – Check the above figure, ls_person has last reference to object employee as it_parent had employee as the 2nd entry. So in the 2nd loop pass, ls_person got employee reference.

Now, when we try to Down Cast ls_person (employee) to r_student, at runtime, it finds the mismatch and the TRY ENDTRY catches the exception.

WHAT IS THE BEST PRACTICE TO HANDLE DOWN CASTING OR WIDENING CASTING?

Ans – Widening Cast can be dangerous as the error message in above case. So when you see the Casting Operator “?=”, you need to be cautious and double check what you are trying to do. As a precautionary step, always do an Up Cast or Narrowing Casting before doing a Down Casting. This simple trick would help you have the LHS and RHS of the same reference type.

Down Casting is practically needed when you want to assign the runtime reference of one Child Class in Parent Class to another reference of same Child Class. Parent Class which has reference of Student 1, can move the reference to another Student 2.

The below one line of code helps to resolve our Down Cast Issue.

Hope, we were able to explain this CASTing concept in Object Oriented Programming. The next time you see “?=” you know what it means. Did we just complete one Polymorphism example?