2007 Jun 29 5:43 AM
HI,
what is mean by interfaces oops ? explain in detail.Give some example
Thank you
Ashok kumar
2007 Jun 29 5:47 AM
hi,
for interfaces
http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm
Interfaces are independent structures that allow you to enhance the class-specific public points of contact by implementing them in classes.
Interfaces can be defined globally in the R/3 repository or locally in ABAP program
Can define exactly same components in Interfaces as in Classes
Unlike classes, Interfaces do not have instances, instead they are implemented by classes
Implemented using INTERFACES statement in the declaration part of the class
INTERFACES statement must be included in the PUBLIC SECTION of the class
Different classes that implement the same interface can all be addressed in the same way.
Interface references allow users to address different classes in the same manner.
Interfaces can also be nested.
Interfaces are the basis for polymorphism in classes, because they allow a single interface method to behave differently in different classes.
If an interface is implemented in the class, the interface components are added in the public section of the class.
A component comp of an interface intf, implemented in a class, becomes a fully-fledged member of that class, with the name intf~comp.
Classes that implement interfaces must implement all of their methods.
METHOD intf~meth.
ENDMETHOD.
Interfaces allow you to use different classes in a uniform way (polymorphism).
Interface References :
Creating Interface Reference Variables
DATA obj TYPE REF TO intf.
Using this reference variable, you can access any of the components defined in the interface
Nested Interfaces
Interface can include one or more interfaces as components, which can contain interfaces themselves.
Compound Interface : It includes other interface as its component.
Elementary Interface : It does not include any interface as a component.
All interface components of a compound interface have the same level
A component interface exists only once even if it is used once more as a component of another component interface.
Aliases : It can be used to assign alias names to the components of component interfaces, thus making them visible within the interface definition.
ALIASES <alias name> FOR intf~comp.
Where, intf = interface name and comp = Component name
Accessing Objects using Interface References:
It is also possible to directly generate an object to which the interface reference
variable points initially. In this case, the TYPE addition of the statement CREATE OBJECT must be used to specify a class that implements the interface. CREATE OBJECT iref TYPE class.
If the interface intf contains an attribute attr and an instance method meth, you can address them as follows:
Using the class reference variable:
Accessing an attribute attr: cref->intf~attr
Calling a method meth: CALL METHOD cref->intf~meth
Using the interface reference variable:
Accessing an attribute attr: iref->attr
Calling a method meth: CALL METHOD iref->meth
Accessing Static Components of Interfaces:
Use the name of the interface to access constants within an interface.
Accessing a constant const: intf=>const.
To access the other static components of an interface, use an object reference or the class class that is implementing the interface.
Accessing a static attribute attr: class=>intf~attr.
Calling a static method meth: CALL METHOD class=>intf~meth.
Example:
an interface is implemented with the method add_employee. Note that the interface is only implemented in the superclass ( The INTERFACE stament), but also used in the subclasses.
The interface in the example only contains a method, but an iterface can also contain attrbutes, constants, types and alias names.
REPORT zbc404_hf_events_3 .
INTERFACE lif_employee
INTERFACE lif_employee.
METHODS:
add_employee
IMPORTING im_no TYPE i
im_name TYPE string
im_wage TYPE i.
ENDINTERFACE.
*******************************************************
Super class LCL_CompanyEmployees
*******************************************************
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,
add_employee "Removed
IMPORTING im_no TYPE i
im_name TYPE string
im_wage TYPE i,
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.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD lif_employee~add_employee.
Adds a new employee to the list of employees
DATA: l_employee TYPE t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
ENDMETHOD.
METHOD display_employee_list.
Displays all employees and there wage
DATA: l_employee TYPE t_employee.
WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_BlueCollar_Employee
*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no TYPE i
im_name TYPE string
im_hours TYPE i
im_hourly_payment TYPE i,
lif_employee~add_employee REDEFINITION..
PRIVATE SECTION.
DATA:no TYPE i,
name TYPE string,
hours TYPE i,
hourly_payment TYPE i.
ENDCLASS.
*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION
CLASS lcl_bluecollar_employee IMPLEMENTATION.
METHOD constructor.
The superclass constructor method must be called from the subclass
constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
hours = im_hours.
hourly_payment = im_hourly_payment.
ENDMETHOD.
METHOD lif_employee~add_employee.
Calculate wage an call the superclass method add_employee to add
the employee to the employee list
DATA: l_wage TYPE i.
l_wage = hours * hourly_payment.
CALL METHOD super->lif_employee~add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_WhiteCollar_Employee
*******************************************************
CLASS lcl_whitecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no TYPE i
im_name TYPE string
im_monthly_salary TYPE i
im_monthly_deductions TYPE i,
lif_employee~add_employee REDEFINITION.
PRIVATE SECTION.
DATA:
no TYPE i,
name TYPE string,
monthly_salary TYPE i,
monthly_deductions TYPE i.
ENDCLASS.
*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION
CLASS lcl_whitecollar_employee IMPLEMENTATION.
METHOD constructor.
The superclass constructor method must be called from the subclass
constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
monthly_salary = im_monthly_salary.
monthly_deductions = im_monthly_deductions.
ENDMETHOD.
METHOD lif_employee~add_employee.
Calculate wage an call the superclass method add_employee to add
the employee to the employee list
DATA: l_wage TYPE i.
l_wage = monthly_salary - monthly_deductions.
CALL METHOD super->lif_employee~add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
R E P O R T
*******************************************************
DATA:
Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
Create bluecollar employee obeject
CREATE OBJECT o_bluecollar_employee1
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_hours = 38
im_hourly_payment = 75.
Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_wage = 0.
Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
Add bluecollar employee to employee list
CALL METHOD o_whitecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
Display employee list and number of employees. Note that the result
will be the same when called from o_whitecollar_employee1 or
o_bluecolarcollar_employee1, because the methods are defined
as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.
Message was edited by:
Sudha Rani Pathuri
2007 Jun 29 5:47 AM
Hi Ashok,
Refer the foll. link .. it ll take u thru the basics of OO-ABAP as well as Interface
https://www.sdn.sap.com/irj/sdn/wiki?path=/display/abap/objectOrientedABAP+(OO-ABAP)&
Hope this helps.
2007 Jun 29 5:50 AM
2007 Jun 29 5:57 AM
2007 Jun 29 6:02 AM
2007 Jun 29 3:22 PM
HI,
see this example.
REPORT ZINTERFACE.
INTERFACE inter1.
DATA:inter1_num TYPE i.
METHODS:inter1_meth importing value(number1) type i.
ENDINTERFACE.
INTERFACE inter2.
DATA:inter2_num TYPE i.
METHODS:inter2_meth importing value(number2) type i.
ENDINTERFACE.
CLASS clas1 DEFINITION.
PUBLIC SECTION.
INTERFACES:inter1,inter2.
DATA:cls_num TYPE i.
METHODS:cls_meth.
ENDCLASS.
CLASS clas1 IMPLEMENTATION.
METHOD inter1~inter1_meth.
cls_num = number1 + number1.
inter1~inter1_num = cls_num + cls_num.
write:/ 'i am from first interface'.
write:/ number1,/ cls_num,/ inter1~inter1_num.
ENDMETHOD.
METHOD inter2~inter2_meth.
cls_num = number2 + number2 + number2.
inter2~inter2_num = cls_num + cls_num + cls_num.
write:/ 'i am from second interface'.
write:/ number2,/ cls_num,/ inter2~inter2_num.
ENDMETHOD.
METHOD cls_meth.
write:/ 'i am from class'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA:myinput TYPE i value 20.
DATA:obj TYPE REF TO clas1.
CREATE OBJECT obj.
CALL METHOD obj->inter1~inter1_meth
exporting number1 = myinput.
CALL METHOD obj->inter2~inter2_meth
exporting number2 = myinput.
CALL METHOD obj->cls_meth.
rgds,
bharat.
2007 Jun 29 5:51 AM
Hi,
Interfaces are similar to classes where in you will not have any METHOD implementations. You will only have Attributes and METHOD definitions.
Interfaces have to be implemented by Classes.
The use is that One interface can be implemented by any number of classes.
SO using an interface reference you can work on MANY TYPES OF CLASS objects.
For example.
DATA: intf type ref to Interface1.
Suppose Class1 class2 ... classn are implemnting this interface1.
then we can
intf = clas1_obj.
intf = clas2_obj.
intf = clasn_obj.
SO one reference can be used to work with many objects of different classes.
Regards,
Sesh
2007 Jun 29 2:33 PM
hi,
interface concepts are related to OOP concepts in abap.. in general interface conatin only the definition of a method..and the implemenation is done in the class.
check out this code..copy and paste it in se38..or check with abapdocu tcode..
REPORT demo_interface.
INTERFACE status.
METHODS write.
ENDINTERFACE.
CLASS counter DEFINITION.
PUBLIC SECTION.
INTERFACES status.
METHODS increment.
PRIVATE SECTION.
DATA count TYPE i.
ENDCLASS.
CLASS counter IMPLEMENTATION.
METHOD status~write.
WRITE: / 'Count in counter is', count.
ENDMETHOD.
METHOD increment.
ADD 1 TO count.
ENDMETHOD.
ENDCLASS.
CLASS bicycle DEFINITION.
PUBLIC SECTION.
INTERFACES status.
METHODS drive.
PRIVATE SECTION.
DATA speed TYPE i.
ENDCLASS.
CLASS bicycle IMPLEMENTATION.
METHOD status~write.
WRITE: / 'Speed of bicycle is', speed.
ENDMETHOD.
METHOD drive.
ADD 10 TO speed.
ENDMETHOD.
ENDCLASS.
DATA: count TYPE REF TO counter,
bike TYPE REF TO bicycle,
status TYPE REF TO status,
status_tab TYPE TABLE OF REF TO status.
START-OF-SELECTION.
CREATE OBJECT: count, bike.
DO 5 TIMES.
CALL METHOD: count->increment,
bike->drive.
ENDDO.
APPEND: count TO status_tab,
bike TO status_tab.
LOOP AT status_tab INTO status.
CALL METHOD status->write.
ENDLOOP.
thanks
jaideep
reward points if it is useful..