Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Interfaces concept ...Urgent?

Hi all,

As we all know multiple inheritance cannot be achieved in ABAP classes, so we need to take INTERFACES approach, I am trying to do from the below mentioned code.

&----


*& INTERFACE Stu_course

&----


INTERFACE stu_course.

DATA : course_id.

METHODS: max_student,

add_student,

drop_student.

ENDINTERFACE.

&----


*& Class cl_course

&----


CLASS cl_course DEFINITION ABSTRACT.

PUBLIC SECTION.

INTERFACES stu_course.

PRIVATE SECTION.

DATA : count TYPE i.

ENDCLASS.

CLASS cl_course IMPLEMENTATION.

METHOD stu_course~add_student.

WRITE: / 'Student added'.

ENDMETHOD.

METHOD stu_course~drop_student.

WRITE: / 'Student Dropped'.

ENDMETHOD.

METHOD stu_course~max_student.

count = count + 1.

IF count GT 10.

write: / 'maximum number of student reached'.

ENDIF.

ENDMETHOD.

ENDCLASS.

&----


*& Class cl_student

&----


CLASS cl_student DEFINITION. "INHERITING FROM cl_university_member.(currently not mentioned)

PUBLIC SECTION.

DATA : student_id type i.

INTERFACES stu_course.

METHODS:register_course

IMPORTING course TYPE REF TO cl_course,

withdraw_course

IMPORTING course TYPE REF TO cl_course.

ENDCLASS.

CLASS cl_student IMPLEMENTATION.

METHOD register_course.

"Here i want to call the definition of method "add_student" of class cl_course without inheritance, so that i can achieve multiple inheritance."

ENDMETHOD.

ENDCLASS.

  • Class cl_student wants to inherit the definition of methods like add_student,drop_student from class cl_course.Is this posible n how?

Thanks n Regards

Rohit

Former Member
Former Member replied

Hi,

Multiple inheritance as a concept is not allowed in ABAP Objects. But the effect and behaviour that you get by multiple inheritance is acheviable in ABAP objects with the help of interfaces. As follows.

An interface can inlcude more than one interface, that is an interface can extend more than one interface. Now if you have say interface1, interface2 and interface3.

interface4 can be declared to include all three other interfaces. And you class can include this interface4 in its interfaces tab.

Due to this your class gets the behviour(methods) that is defined in all ther interfaces. You can assign an object of this class to a reference of any one of these four interfaces. So one object can react to method calls on four different interfaces which is what you want to achevie in multiple inheritance.

REPORT demo_inheritance.

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS: set IMPORTING value(set_value) TYPE i,

increment,

get EXPORTING value(get_value) TYPE i.

PROTECTED SECTION.

DATA count TYPE i.

ENDCLASS.

CLASS counter IMPLEMENTATION.

METHOD set.

count = set_value.

ENDMETHOD.

METHOD increment.

ADD 1 TO count.

ENDMETHOD.

METHOD get.

get_value = count.

ENDMETHOD.

ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.

PUBLIC SECTION.

METHODS increment REDEFINITION.

DATA count_ten(1) TYPE c.

ENDCLASS.

CLASS counter_ten IMPLEMENTATION.

METHOD increment.

DATA modulo TYPE i.

CALL METHOD super->increment.

WRITE / count.

modulo = count MOD 10.

IF modulo = 0.

count_ten = count_ten + 1.

WRITE count_ten.

ENDIF.

ENDMETHOD.

ENDCLASS.

DATA: count TYPE REF TO counter,

number TYPE i VALUE 5.

START-OF-SELECTION.

CREATE OBJECT count TYPE counter_ten.

CALL METHOD count->set EXPORTING set_value = number.

DO 20 TIMES.

CALL METHOD count->increment.

ENDDO.

http://www.intelligententerprise.com/channels/applications/feature/archive/keller.jhtml

http://help.sap.com/saphelp_me21sp2/helpdata/en/c3/225b5c54f411d194a60000e8353423/frameset.htm

multiple inheritance

Does ABAP Obj. supports multiple or multilevel inheritence

Please give me reward points if it is useful

Thanks

Murali Poli

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question