Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

abap objects

Former Member
0 Kudos

i want to redefine my constructor of super class in my base class in global class defination ,how can i do it .since we cannot define tow constructors in same class.

4 REPLIES 4

Former Member
0 Kudos

HI James,

We cannot have two constructors in Same class,

but we can do that with different parameters .

Regards.

Laxmi

Former Member
0 Kudos

hi

chk this out

CLASS C_BIKE DEFINITION ABSTRACT.

PUBLIC SECTION.

METHODS: DRIVE ABSTRACT,

STOP.

PROTECTED SECTION.

DATA SPEED TYPE I.

ENDCLASS.

CLASS C_BIKE IMPLEMENTATION.

METHOD STOP.

SPEED = 0.

WRITE: / 'Bike speed =', SPEED.

ENDMETHOD.

ENDCLASS.

CLASS C_BICYCLE DEFINITION INHERITING FROM C_BIKE.

PUBLIC SECTION.

METHODS DRIVE FINAL REDEFINITION.

ENDCLASS.

CLASS C_BICYCLE IMPLEMENTATION.

METHOD DRIVE.

SPEED = SPEED + 10.

WRITE: / 'Bicycle speed =', SPEED.

ENDMETHOD.

ENDCLASS.

CLASS C_MOTORCYCLE DEFINITION INHERITING FROM C_BIKE.

PUBLIC SECTION.

DATA IGNITION(1) TYPE C VALUE 'X'.

METHODS: DRIVE FINAL REDEFINITION,

STOP FINAL REDEFINITION.

ENDCLASS.

CLASS C_MOTORCYCLE IMPLEMENTATION.

METHOD DRIVE.

IF IGNITION = 'X'.

SPEED = SPEED + 100.

WRITE: / 'Motorcycle speed =', SPEED.

ENDIF.

ENDMETHOD.

SUPER->STOP( ).

IGNITION = ' '.

ENDMETHOD.

ENDCLASS.

DATA: BIKE1 TYPE REF TO C_BIKE,

BIKE2 TYPE REF TO C_BIKE.

START-OF-SELECTION.

CREATE OBJECT: BIKE1 TYPE C_BICYCLE,

BIKE2 TYPE C_MOTORCYCLE.

BIKE1->DRIVE( ).

BIKE2->DRIVE( ).

...

BIKE1->STOP( ).

BIKE2->STOP( ).

also refer to this

Inheritance and Constructors

Constructors are subject to special inheritance rules.

Instance Constructors

Each class has an instance constructor called CONSTRUCTOR. This name does not comply with the rule of unique component names within an inheritance tree. Consequently, the instance constructors of the individual classes of an inheritance tree are totally independent of each other. Instance constructors of superclasses cannot be redefined in subclasses. Anyway, they cannot be called explicitly using CALL METHOD CONSTRUCTOR. This avoids namespace conflicts.

The instance constructor of a class is executed when you create an object using the CREATE OBJECT statement. Since a subclass inherits all attributes from its superclasses that are visible to it, and since the content of these attributes can be set by instance constructors of its classes, the instance constructor of a subclass is responsible for ensuring that the instance constructors of all superclasses are executed as well. This requires that the instance constructor of each subclass contains a SUPER->CONSTRUCTOR( ) or CALL METHOD SUPER->CONSTRUCTOR call of the instance constructor of the direct superclass. Exceptions to this rule are the direct subclasses of the root node OBJECT.

In superclasses that do not have an explicitly defined instance constructor, the implicit instance constructor is executed. This constructor automatically ensures that the instance constructor of the next higher superclass is called.

When instance constructors are called, all non-optional interface parameters of these constructors must be populated. There are several ways to do this:

Parameter population at CREATE OBJECT

If the class of the object created has an instance constructor with an interface, this interface must be populated with EXPORTING.

If the class of the object created has an instance constructor without an interface, no parameters are passed.

If the class of the object created does not have an explicit instance constructor, the system must pass through the inheritance tree to proceed to the next superclass which has an explicit instance constructor. If this constructor has an interface, this interface must be populated with EXPORTING. Otherwise, no parameters are passed.

Parameter population at [CALL METHOD] SUPER->CONSTRUCTOR

If the direct superclass has an instance constructor with an interface, this interface must be populated with EXPORTING.

If the direct superclass has an instance constructor without an interface, no parameters are passed.

If the direct superclass does not have an explicit instance constructor, the system must pass through the inheritance tree to proceed to the next superclass which has an explicit instance constructor. If this constructor has an interface, this interface must be populated with EXPORTING. Otherwise, no parameters are passed.

Both at CREATE OBJECT and at [CALL METHOD] SUPER->CONSTRUCTOR, the next possible explicit instance constructor must be taken into account. If one exists, its interface must be populated. The same applies to exception handling in instance constructors. Using inheritance requires in-depth knowledge of the entire inheritance tree since when an object of a class is created that resides on a deep level of the tree, parameters may have to be passed to the constructor of a class that resides closer to the root node.

The instance constructor of a subclass is split into two parts by the [CALL METHOD] SUPER->CONSTRUCTOR call, which is required from a syntactical point of view. In the statements before the call, the constructor behaves like a static method. This means it cannot access the instance attributes of its class. It is only after the call that instance attributes can be addressed. The statements before the call are used to determine actual parameters for the interface of the instance constructor of a superclass. Only static attributes or local data can be used for this.

When a subclass is instantiated, the system makes a nested call of the instance constructors from the subclass to the superclasses where the instance attributes can only be addressed at the deepest nesting level, that is, in the highest superclass. When the system returns to the constructors of the underlying subclasses, their instance attributes can then also be successively addressed.

The methods of subclasses are not visible in constructors. If an instance constructor calls an instance method of the same class using the implicit self-reference ME->, the method is called as it is implemented in the class of the instance constructor and not as the possibly redefined method of the subclass to be instantiated. This is an exception to the rule that when instance methods are called the implementation is always called in the class to whose instance the reference currently points.

Static Constructors

Each class has a static constructor called CLASS_CONSTRUCTOR. As far as the namespace in an inheritance tree is concerned, the static constructor is subject to the same rules as the instance constructor.

The static constructor is executed when a subclass is first addressed within a program. However, all preceding static constructors of the entire inheritance tree must have been executed before. Otherwise, a static constructor can only be called once at the runtime of the program. This is why when a subclass is addressed for the first time the system searches the next higher superclass whose static constructor has not yet been executed. This static constructor is executed first. Afterwards the constructors of all subsequent subclasses are executed successively down to the subclass addressed.

Former Member
0 Kudos

Former Member
0 Kudos

constructors can not be inherited.

so you can not redefine the constructors.

you can write in the subclass like this

METHOD CONSTRUCTOR.

CALL METHOD SUPER --> CONSTRUCTOR.

you can write the extra logic here.

END METHOD.

Message was edited by: Vijay Kumar Reddy