| Home • Tips • Tutorials • Forums • Certification Q's • Interview Q's • Jobs • Testimonials • Contact Us | ||
Document Categories:
What's New?
Contribute?Sample SpecsWhat's Hot? |
Demo on "Narrow Casting"By Malya Ajimera, Capgemini consulting Definition:
The assignment of a
subclass instance to a reference variable of the type "reference to super
class" is described as a narrowing cast, because you are switching from a
more detailed view to a one with less detail. It is also called as up-casting. Use
of narrowing casting: A user who
is not interested in the finer points of cars, trucks, and busses (but only, for
example, in the fuel consumption and tank gauge) does not need to know about
them. This user only wants and needs to work with (references to) the
lcl_vehicle(super class) class. However, in order to allow the user to work with
cars, busses, or trucks, you generally need a narrowing cast. Principle
of narrowing casting:
Example: Super
class: vehicle (contains
general methods)
Sub class:
truck (contains more specific methods) Here
method4 is the specific for the sub class and remaining methods are inherited
from the super class. Now
create the object with reference to the subclass.
DATA: REF_TRUCK TYPE REF TO
TRUCK.
CREATE OBJECT REF_TRUCK. Narrowing
cast:
DATA: REF_VEHICLE TYPE REF TO VEHICLE.
REF_VEHICLE = REF_TRUCK. Accessing
methods using super class reference. 1. By the
super class reference (REF_VEHICLE) it is possible to access all the methods
which are defined at the super class but the implementations are taken from the
sub class. 2. If any
method is redefined at the sub class then that method’s implementation which
exist at the sub class is taken in to consideration. E.g.
assume that ‘method2’ is redefined at the sub class. When this
method is accessed using the super class reference Like: Call
method REF_VEHICLE->method2. Here we
can access the implementation which exists at the sub class but not from the
super class. 3. It is
not possible to access the methods which only defined in the sub class using the
super class reference. E.g.
Method4 is not accessed using reference REF_VEHICLE. Call
method REF_VEHICLE-> Method4. This is
wrong convention. Demo
for narrowing casting: Go to
transaction SE38.
Now
execute (F8):
Result:
|
|
|
Please send us your feedback/suggestions at webmaster@SAPTechnical.COM Home • Contribute • About Us • Privacy • Terms Of Use • Disclaimer • Safe • Companies: Advertise on SAPTechnical.COM | Post Job • Contact Us ©2006-2007 SAPTechnical.COM. All rights reserved. All
product names are trademarks of their respective companies. SAPTechnical.COM
is in no way affiliated with SAP AG. Graphic Design by Round the Bend Wizards |
||