‎2007 Jun 27 8:36 AM
can any some detailed information,faq's regarding only abstract classes in abap,not other than that.I will be very thankful to those who have done it.
‎2007 Jun 27 8:40 AM
Hi,
ABSTRACT CLASSES are those classes which are concidered non-complete, we cannot instantiate an ABSTRACT class.
It can only be inherited and its methods which does not have implementation will be provided implementation in the SUB CLASS.
Regards,
Sesh
‎2007 Jun 27 12:47 PM
Hi,
A class defined as ABSTRACT cannot be instantiated , that is one cannot use CREATE OBJECT with reference to the class.
Abstract class can only be accessed using its static components or its subclasses.
Abstract class serves as a template for subclasses
If a class contains any abstract method the whole class becomes abstract.
A method, which is abstract, should be redefined in derived class.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: do_something
IMPORTING ...i1 TYPE
EXPORTING
e1 TYPE
CHANGING
c1 TYPE
EXCEPTIONS
en.
PRIVATE SECTION.
DATA:
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD do_something.
ENDMETHOD.
ENDCLASS.
The use of abstraction through abstract classes is restricted to the sub classes of a specific node within the inheritance tree.
An Abstract class can declare and implement non abstract methods which its subclasses can use with or without redefining them.
Regards,
Kate
‎2007 Jun 27 1:19 PM
Hi Sandeep,
Abstract classes cannot be instantiated.
Superclasses are typical use for abstract classes as they are not intended to be
instantiated, but serve as models for subclasses.
Only abstract classes can contain ABSTRACT methods. These are methods with
no implementation in the class where defined. If the subclass of the abstract
class is not abstract then the abstract methods must be redefined there.
Reference to such abstract classes can therefore be used for polymorphic
access to subclass instances.
Static methods cannot be abstract because they cannot be redefined.
Abstract class can have non-abstract methods also.
<u><b>SAMPLE CODE</b></u>
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of Abstract class *
Abstarct classes are classes that cannot be instantiated. *
Instead we can only address the class using its static *
components or its subclasses. The major purpose of an *
abstract class is to serve as a template for subclasses. *
----
CLASS zl_lcl_vehicle DEFINITION ABSTRACT.
PUBLIC SECTION.
An abstract method is defined in an abstract class and cannot be *
implemented in that class. If the subclass of that class is not *
abstract the abstract methods must be implemented there for the *
first time. If a subclass does not implement all of the abstract *
methods of its superclasses, it must itself declare them abstract. *
Static Methods can't be abstract because they cant be redefined. *
Constructors can't be abstract because they can't be inherited *
METHODS: set_make ABSTRACT IMPORTING im_make TYPE string
im_model TYPE string,
get_make ABSTRACT EXPORTING ex_make TYPE string
ex_model TYPE string.
PRIVATE SECTION.
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Definiton) zl_lcl_car
&----
Definition of Subclass *
----
CLASS zl_lcl_car DEFINITION INHERITING FROM zl_lcl_vehicle.
PUBLIC SECTION.
To implement an abstract method in a subclass, it must be redefined *
in the subclass using the REDEFINITION addition. *
METHODS: set_make REDEFINITION,
get_make REDEFINITION,
get_veh_type EXPORTING ex_vehtype TYPE string.
PRIVATE SECTION.
Since the private components of superclass are not visible in *
subclass, private components are declared in the subclass that have *
the same names as private components of the superclass. *
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_car DEFINITION
&----
*& Class (Implementation) ZL_LCL_VEHICLE
&----
Implementation of Abstract class
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
Since this class contains only abstract methods which cant be *
implemented here, there is no implementation of this class. *
If abstract class has some non-abstract methods, those can be *
implemented here *
ENDCLASS. "ZL_LCL_VEHICLE
&----
*& Class (Implementation) zl_lcl_car
&----
Implementation of Subclass
----
CLASS zl_lcl_car IMPLEMENTATION.
METHOD set_make.
Methods that a subclass inherits from a superclass use the private *
attributes of the superclass, and not any private components of the *
subclass with the same names. *
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
gv_make = im_make.
gv_model = im_model.
ENDIF.
ENDMETHOD. "set_make
METHOD get_make.
ex_make = gv_make.
ex_model = gv_model.
ENDMETHOD. "get_make
METHOD get_veh_type.
ex_vehtype = 'car'.
ENDMETHOD. "get_veh_type
ENDCLASS. "zl_lcl_car
Declaring a reference variable with reference to the subclass
DATA z_car TYPE REF TO zl_lcl_car.
DATA : gv_make TYPE string,
gv_model TYPE string,
gv_veh_type TYPE string.
START-OF-SELECTION.
Create instance of the vehicle class
CREATE OBJECT z_car.
z_car->set_make( EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO' ).
z_car->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
z_car->get_veh_type( IMPORTING ex_vehtype = gv_veh_type ).
WRITE: / 'Vehicle Type :', gv_veh_type,
/ 'Make :', gv_make,
/ 'Model :', gv_model.
Award points if found useful.
Regards
Indrajit