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: 

Interfaces concept ...Urgent?

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

Please give me reward points if it is useful

Thanks

Murali Poli

2 REPLIES 2

Former Member
0 Kudos

hi rohit,

see below details regarding Intefaces,

Interface

Simple use of an interface

Theme This program will show simple use of an interface with its own data and methods and how it is implemented in a class. It will also show that there can be methods of same name for an interface and the class implementing the interface.

Program Desc This program contains an interface I1 with attribute : NUM an method : METH1.

This interface is implemented in a class : C1 which also has its own method METH1.

An object OREF is created from class C1 and both the methods METH1 , one for class and another for interface is called using the object.

Dump

report ysubdel .

interface i1.

data : num type i .

methods : meth1.

endinterface.

class c1 definition.

public section.

methods : meth1. “ class C1’s own method

interfaces : i1.

endclass.

class c1 implementation.

method : meth1.

write:/5 'I am meth1 in c1'.

endmethod.

method i1~meth1.

write:/5 'I am meth1 from i1'.

endmethod.

endclass.

start-of-selection.

data : oref type ref to c1.

create object oref.

write:/5 oref->i1~num.

call method oref->meth1.

call method oref->i1~meth1.

Output 0

I am meth1 in c1

I am meth1 from i1

Interfaces can only be implemented in the public section of a class

Theme This program will show you that classes implementing an interface can only contain the features of the interface in its public section.

Program Description In this program, class C1 is trying to accommodate interface I1 in its PRIVATE SECTION.

This creates a compilation error, establishing the theme.

Dump report ysubdel .

interface i1.

methods : meth1.

endinterface.

class c1 definition.

protected section.

interfaces : i1.

endclass.

Output Compilation error with error message :-

INTERFACES may only be implemented in the public section.

A class with an interface should implement all the methods of that interface

Theme This program will show that a class containing an interface should implement all the methods of the interface in its implementation section.

Program Descrip Class C1 implements interface I1, which has got two methods , METH1 and METH2. But, in the IMPLEMENTATION section of class C1, only METH1 is implemented.

This program will create a compilation error, establishing the theme.

Dump

report ysubdel .

interface i1.

methods : meth1 ,

meth2 .

endinterface.

class c1 definition.

public section.

interfaces : i1.

endclass.

class c1 implementation.

method i1~meth1.

write:/5 'I am meth1 from i1'.

endmethod.

endclass.

start-of-selection.

data : oref type ref to c1.

create object oref.

call method oref->i1~meth1.

Output Compilation error with error message :-

Implementation missing for method “I1~METH2”

if helpful please reward the points,

Aruna

Former Member
0 Kudos

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

Please give me reward points if it is useful

Thanks

Murali Poli