Application Development and Automation 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: 
Read only

Regarding Super classes

i045323
Product and Topic Expert
Product and Topic Expert
0 Likes
2,135

Hi,

I have created one class in SE24. This class will be used as a super class for other classes.

When subclasses are derived from this superclass, i want to make sure that some of the methods of superclasses are redefined by the subclasse compulsarily.

So i want to force the subclasses to redefine complusarily some of the methods of its super class.

Is this feasible. If so please let me know the corresponding approach.

Thanks in advance !

Pramod

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,469

Hi Pramod,

ABSTRACT methods are for the same purpose. If u make it an abstarct method then it has to be implemented/redefined in subclass. For that you have to make superclass as ABSTRACT class too then you can not instantiate your superclass.

Regards,

Vishal.

4 REPLIES 4
Read only

Former Member
0 Likes
1,469

Hi,

U can make the super class as friend class if possible and inherit its method.

Programatically, you can inherit all the methods of super class in sub class using inheriting from as extension.

Regards,

Ameet

Read only

Former Member
0 Likes
1,469

Hi,

Check this out this will help you.

Inheritance is the concept of passing the behavior of a class to another class.

1.You can use an existing class to derive a new class.

2.Derived class inherits the data and methods of a super class.

3.However they can overwrite the methods existing methods and also add new once.

4.Inheritance is to inherit the attributes and methods from a parent class.

Inheritance:

Inheritance is the process by which object of one class acquire the properties of another class.

Advantage of this property is reusability.

This means we can add additional features to an existing class with out modifying it.

Go to SE38.

Provide the program name.

Provide the properties.

Save it.

Provide the logic for inheritance.


*&-----------------------------------------------------*
*& Report  ZLOCALCLASS_VARIABLES                      *
*&                                                    *
*&-----------------------------------------------*
*&                                                    *
*&                                                  *
*&----------------------------------------------------*REPORT  ZLOCALCLASS_VARIABLES.

*OOPS INHERITANCE
*SUPER CLASS FUNCTIONALITY
*DEFINE THE CLASS.

CLASS CL_LC DEFINITION.
PUBLIC SECTION.
DATA: A TYPE I,
      B TYPE I,
      C TYPE I.
METHODS: DISPLAY,
         MM1.
CLASS-METHODS: MM2.
ENDCLASS.

*CLASS IMPLEMENTATION
CLASS CL_LC IMPLEMENTATION.
METHOD DISPLAY.
WRITE:/ 'THIS IS SUPER CLASS' COLOR 7.
ENDMETHOD.
METHOD MM1.
WRITE:/ 'THIS IS MM1 METHOD IN SUPER CLASS'.
ENDMETHOD.
METHOD MM2.
WRITE:/ 'THIS IS THE STATIC METHOD' COLOR 2.
WRITE:/ 'THIS IS MM2 METHOD IN SUPER CLASS' COLOR 2.
ENDMETHOD.
ENDCLASS.

*SUB CLASS FUNCTIONALITY
*CREATE THE CLASS.
*INHERITING THE SUPER CLASS.
CLASS CL_SUB DEFINITION INHERITING FROM CL_LC. "HOW WE CAN INHERIT
PUBLIC SECTION.
DATA: A1 TYPE I,
      B1 TYPE I,
      C1 TYPE I.
METHODS: DISPLAY REDEFINITION,     "REDEFINE THE SUPER CLASS METHOD
         SUB.
ENDCLASS.

*CLASS IMPLEMENTATION.
CLASS CL_SUB IMPLEMENTATION.
METHOD DISPLAY.
WRITE:/ 'THIS IS THE SUB CLASS OVERWRITE METHOD' COLOR 3.
ENDMETHOD.
METHOD SUB.
WRITE:/ 'THIS IS THE SUB CLASS METHOD' COLOR 3.
ENDMETHOD.
ENDCLASS.

*CREATE THE OBJECT FOR SUB CLASS.
DATA: OBJ TYPE REF TO CL_SUB.
START-OF-SELECTION.
CREATE OBJECT OBJ.
CALL METHOD OBJ->DISPLAY. "THIS IS SUB CLASS METHOD
CALL METHOD OBJ->SUB.
WRITE:/'THIS IS THE SUPER CLASS METHODS CALLED BY THE SUB CLASS OBJECT'COLOR 5.
SKIP 1.
CALL METHOD OBJ->MM1.     "THIS IS SUPER CLASS METHOD
CALL METHOD OBJ->MM2.

*CREATE THE OBJECT FOR SUPER CLASS.
DATA: OBJ1 TYPE REF TO CL_LC.
START-OF-SELECTION.
CREATE OBJECT OBJ1.
SKIP 3.
WRITE:/ 'WE CAN CALL ONLY SUPER CLASS METHODS BY USING SUPER CLASS OBJECT' COLOR 5.
CALL METHOD OBJ1->DISPLAY. "THIS IS SUPER CLASS METHOD
CALL METHOD OBJ1->MM1.
CALL METHOD OBJ1->MM2.

This example will help you to solve your problem.

For more detailed information GOTO -> SAPTECHNICAL ->Tutorials -> Object Oriented Programming.

Regards Madhu.

Code Formatted by: Alvaro Tejada Galindo on Jan 7, 2009 12:13 PM

Read only

Former Member
0 Likes
1,470

Hi Pramod,

ABSTRACT methods are for the same purpose. If u make it an abstarct method then it has to be implemented/redefined in subclass. For that you have to make superclass as ABSTRACT class too then you can not instantiate your superclass.

Regards,

Vishal.

Read only

i045323
Product and Topic Expert
Product and Topic Expert
0 Likes
1,469

Hi,

These methods needs to be called in the constructor method. If they are made abstract, then they cannot be called in the constructor.

What is the best way to overcome this?