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

ABAP

Former Member
0 Likes
355

Hi Team,

What is the difference between Class interface and Function module.

Thanks

Suresh

2 REPLIES 2
Read only

Former Member
0 Likes
327

Main difference between a CLASS and an INTERFACE is:

- Class has both definition and an implementation whereas Interface only has a definition.

Interfaces are actually implemented via a Class. (Due to this way of implementing Interfaces, the ABAP Objects supports the concept of multiple inheritances.)

Class

1.1 Accessibility of different sections of a classFrom this program, one will learn:-

1. How to define, implement and instantiate a class.

2. What are the different sections of visibility in a class.

3. How to define instance attributes and get them accessed by external users.

The following program will also show that :-

 Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.

 Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.

 Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.

Brief Description This program contains a class : parentclass with following attributes in different sections:-

Commondata in public section

Protectdata in private section

Privatedata in private section

The method showval in class : parentclass displays values of all the attributes.

This demonstrates that class can access all its attributes.

Class childclass is a subclass of class parentclass, which has a method : subval.

It displays the value for the data : commondata and protectdata .

Then, it changes the values for both and displays them again.

This demonstrates that subclass can access/change public/ protected attributes of superclass.

In the START-OF-SELECTION event, object : parent is instantiated from class : parentclass and object : child is instantiated from class : childclass.

Then , the method showval of parent(object of parentclass) and method subval of child(object of childclass) is called , which displays the values of different attributes.

Then, the public attribute of object parent is changed and the changed value is displayed.

This demonstrates that external users can change/display public attributes of a class.

Dump of the program:-

REPORT YSUBDEL LINE-SIZE 120.

CLASS parentclass DEFINITION .

PUBLIC SECTION.

DATA : commondata(30) type c value 'Accessible to all'.

METHODS : SHOWVAL.

PROTECTED SECTION.

DATA : protectdata(40) type c value 'Protected data'.

private section.

data : privatedata(30) type c value 'Private data'.

ENDCLASS.

CLASS parentclass IMPLEMENTATION.

METHOD : SHOWVAL.

write:/5 'All data from parentclass shown:-'.

write:/ sy-uline.

WRITE:/5 COMMONDATA,

/5 PROTECTDATA,

/5 PRIVATEDATA.

endmethod.

endclass.

CLASS childclass DEFINITION INHERITING FROM parentclass.

PUBLIC SECTION .

METHODS : subval.

ENDCLASS.

CLASS childclass IMPLEMENTATION.

method : subval.

skip 1.

write:/5 'Data of parent shown from child-'.

write:/5 sy-uline.

WRITE:/5 COMMONDATA,

/5 PROTECTDATA.

Commondata = 'Public data changed in subclass'.

Protectdata = 'Protected data changed in subclass'.

write:/5 sy-uline.

WRITE:/5 COMMONDATA,

/5 PROTECTDATA.

endmethod.

endclass.

START-OF-SELECTION.

DATA : parent type ref to parentclass ,

child type ref to childclass .

create object : parent ,

child .

call method : parent->showval ,

child->subval.

skip 2.

parent->commondata = ‘User changing public data’.

write:/5 parent->commondata.

Interface

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.

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.

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.

********************************

Function modules are procedures that are defined in function groups (special ABAP programs with type F) and can be called from any ABAP program.Function modules allow you to encapsulate and reuse global functions in the R/3 System. Tcode is SE37

Classes are templates for objects. The attributes of objects are defined by the components of the class, which describe the state and behavior of objects. They are stored centrally in class pools in the class library in the R/3 Repository. TCODe is SE24

Class is comparable to FUNCTION GROUP.

Method is comparabble with FM.

The main difference between Class and function groups is that although a program can work with the instances of several function groups at the same time, it cannot work with several instances of a single function group means

A Function group can not have more than on einsatnces whereas A Class can have any number of insatbces.

NOTE:When you call a function module, an instance of its function group plus its data, is loaded into the memory area of the internal session. An ABAP program can load several instances by calling function modules from different function groups.

Regards

Vasu

Read only

matt
Active Contributor
0 Likes
327

No simple answer. A class is an object oriented concept, function modules (and function groups) are procedural programming concepts. A function group can be considered to be a singleton class. Each function module could be thought of as a method, and the global data can be thought of as the attributes.

However, classes offer far more:

You can have more than one class instantiated, each with its own attributes.

You can have events on classes.

You get polymorphism and inheritance with classes.

The use of classes means you can program in an object oriented style - when done properly it leads to quicker development, fewer bugs, and better program design, thereby reducing maintenance costs.

matt