Você está na página 1de 4

c 


 
c 
VV VVV

Variables of the type reference to superclass can also refer to subclass


instances at runtime. If you assign a subclass reference to a superclass reference,
this ensures that all components that can be accessed syntactically after the cast
assignment are actually available in the instance. The subclass always contains at
least the same components as the superclass. After all, the name and the signature
of redefined methods are identical.

Suppose, there is a super class named as lcl_super_classs, and lcl_sub_class


is inheriting from lcl_super_class. In this case if we want to access the methods of
lcl_sub_class using the reference of lcl_super_class, then we assign the reference
of subclass to the reference of superclass, this process of assigning sub class
reference to super class reference is known as up-casting . This process is also
known as wide casting because it widen the scope of the reference of super class.

In this case, first we create the reference of both the classes,


½   
    
½  
   

then we create a object of subclass,


   
ënow we perform the up-casting operation.
 

ënow we can use ref_super ( reference variable of super class) to access the
methods of sub class, but we can access only redefine methods or inherited
methods.
If you assign a subclass reference to a super class reference, this ensures that
all components that can be accessed syntactically after the cast assignment are
actually available in the instance. The subclass always contain at least the same
components as the super class. After all, the name and the signature of redefined
methods are identical.
User can therefore address only those methods and attributes from the
subclass instance that they could from the super class instance,
So in the above example ref_super reference when call show method, it
actually access the implementation of show method from lcl_sub_class.

½  
Down-cast (Narrowing Cast) Variables of the type reference to superclass
can also refer to subclass instances at runtime. You may now want to copy such a
reference (back) to a suitable variable of the type reference to subclass. If you want
to assign a superclass reference to a subclass reference, you must use the down-
cast assignment operator MOVE ... ?TO ... or its short form ?=. Otherwise, you
would get a message stating that it is not certain that all components that can be
accessed syntactically after the cast assignment are actually available in the
instance. As a rule, the subclass class contains more components than the
superclass. After assigning this type of reference (back) to a subclass reference to
the implementing class, clients are no longer limited to inherited components:
V

V
V
V

In the above example, lcl_super_class is a super class and lcl_sub_class is


sub class inheriting from lcl_super_class. If we want to access the redefine
methods of the sub class using the reference if the super class then we use
upcasting, but the problem with up-casting is that its scope is only limited inherited
and redefine methods, we can not access the specific methods of sub class (like
getdata of lcl_sub_class). So to access these methods we use down-casting.
Down-casting is always use after the up-casting . first we have to create
reference of both classes.

½   
    
½  
   
½  
   


Then create a object of subclass.



   

Up-casting : pass the reference of sub class to the super class.


Ref_super = ref_sub.
Now using this reference we can access all the inherited and redefine
methods of sub class.
Down-casting: pass the above reference of super class back to the reference
of sub class.
Ref_sub2 ?= ref_super.
Now using down-casting we can access all the methods of subclass included
specific methods of subclass.

Você também pode gostar