Interfaces concept ...Urgent?
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
DATA : course_id.
*& Class cl_course
CLASS cl_course DEFINITION ABSTRACT.
DATA : count TYPE i.
CLASS cl_course IMPLEMENTATION.
WRITE: / 'Student added'.
WRITE: / 'Student Dropped'.
count = count + 1.
IF count GT 10.
write: / 'maximum number of student reached'.
*& Class cl_student
CLASS cl_student DEFINITION. "INHERITING FROM cl_university_member.(currently not mentioned)
DATA : student_id type i.
IMPORTING course TYPE REF TO cl_course,
IMPORTING course TYPE REF TO cl_course.
CLASS cl_student IMPLEMENTATION.
"Here i want to call the definition of method "add_student" of class cl_course without inheritance, so that i can achieve multiple inheritance."
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
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.
CLASS counter DEFINITION.
METHODS: set IMPORTING value(set_value) TYPE i,
get EXPORTING value(get_value) TYPE i.
DATA count TYPE i.
CLASS counter IMPLEMENTATION.
count = set_value.
ADD 1 TO count.
get_value = count.
CLASS counter_ten DEFINITION INHERITING FROM counter.
METHODS increment REDEFINITION.
DATA count_ten(1) TYPE c.
CLASS counter_ten IMPLEMENTATION.
DATA modulo TYPE i.
CALL METHOD super->increment.
WRITE / count.
modulo = count MOD 10.
IF modulo = 0.
count_ten = count_ten + 1.
DATA: count TYPE REF TO counter,
number TYPE i VALUE 5.
CREATE OBJECT count TYPE counter_ten.
CALL METHOD count->set EXPORTING set_value = number.
DO 20 TIMES.
CALL METHOD count->increment.
Please give me reward points if it is useful