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 interface

Former Member
0 Likes
618

hi,

can we write the interface like below

v1 type ref to <interface name>

plz rectify this doubt

very urgent

4 REPLIES 4
Read only

asik_shameem
Active Contributor
0 Likes
562

Hi,

*u cant directly create object and u cant implement the methods in the object using interfaces.

*u have declare that interface inside a class and u have to implement all the methods in interface using that class only.

*go thru in this

INTERFACE lif_employee.

METHODS:

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i.

ENDINTERFACE.

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

INTERFACES lif_employee.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

METHODS:

constructor,

display_employee_list,

display_no_of_employees.

PRIVATE SECTION.

CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

Read only

Former Member
0 Likes
562

Hi,

This program will show the use of interface reference variable and how it can be used to access the components of an interface in a class(implementing that interface). Use of interface reference variable paves the way for polymorphism via interface.

report ysubdel .

interface i1 .

constants : c_name(4) type c value 'ABAP'.

data : inum type i .

class-data : cnum type i .

methods : m1 .

class-methods : m2.

endinterface.

class c1 definition .

public section.

interfaces : I1 data values inum = 5 cnum = 6 .

endclass.

class c1 implementation.

method i1~m1.

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

endmethod.

method i1~m2.

write:/5 'I am class method m2 in c1'.

endmethod.

endclass.

start-of-selection.

data : iref type ref to i1 ,

oref type ref to c1 .

create object : oref.

write:/5 oref->i1~inum ,

oref->i1~cnum ,

c1=>i1~cnum .

call method : oref->i1~m1 ,

oref->i1~m2 ,

c1=>i1~m2 .

write:/5 sy-uline .

iref = oref .

write:/5 iref->inum ,

iref->cnum ,

i1=>c_name .

call method : iref->m1 ,

Regards,

Omkar.

Read only

0 Likes
562

in which situation we are going to use interfaces.

could u plz explain clearly

Read only

Former Member
0 Likes
562

Dear Venkat,

If I understand you correctly, you want to address objects using Interface references. If thats the case then refer to the documentation below:

Addressing Objects Using Interface References

If a class class implements an interface intf, you can use the following assignment between the class reference variable crefand an interface reference irefto make the interface reference in irefpoint to the same object as the class reference in cref:

iref = cref

If a class class implements an interface intf, you do not need to create a class reference variable cref with reference to the class first in order to create an object of the class class. Instead, you can use the TYPE addition in the CREATE OBJECT statement to create an instance of the class with an interface reference variable.

CREATE OBJECT iref TYPE class.

This creates an instance of the class classto which the reference in irefpoints.

If the interface intf contains an instance attribute attr and an instance method meth, you can address the interface components as follows:

>Using the class reference variable cref:

>>· To access an attribute attr: cref->intf~attr

>>· To call a method meth: CALL METHOD >>cref>intf~meth

>Using the interface reference variable iref:

>>· To access an attribute attr: iref->attr

>>· To call a method meth: CALL METHOD iref->meth

As far as the static components of interfaces are concerned, you can only use the interface name to access constants:

To access a constant const: intf=>const

For all other static components of an interface, you can only use object references or the class class that implements the interface:

To access a static attribute attr: class=>intf~attr

To call a static method meth: CALL METHOD class=>intf~meth

Best Regards,

Rajesh

Please reward points if found helpful.