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: 

difference between class and interface

Former Member
0 Kudos

Hi experts,

Can any one please tell me what is the difference between an interface and a class.

Please let me know the answer for the same with a small example.

1 ACCEPTED SOLUTION

Former Member

Hi,

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.)

Regards,

Sumant.

4 REPLIES 4

Former Member

Hi,

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.)

Regards,

Sumant.

Former Member
0 Kudos

hi,

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.

a class contains attribute, methods, events as its components.

<b>we can create instance to the class.</b>

we can create classes locally and globally.

local class create directly in our program where as global classes created in class builder se37.

    • class definition secetion

CLASS <class> DEFINITION.

PUBLIC SECTION.

...

PROTECTED SECTION.

...

PRIVATE SECTION.

...

ENDCLASS.

**class implementation section

CLASS <class> IMPLEMENTATION.

ENDCLASS.

<u><b>INTERFACE.</b></u>

Interface contains only method prototype only, it doesn't provide implementation to its method, we can provide implemetation to its method in our class for that one we can implement the interface.

interface is the source for polymorphism.

we can define interface by using syntax

INTERFACE <intf>.

...

ENDINTERFACE.

<b>we can not create an instance to the interface.</b>

<u><b>

example for class.</b></u>

CLASS C_COUNTER DEFINITION.

PUBLIC SECTION.

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I,

INCREMENT_COUNTER,

GET_COUNTER EXPORTING VALUE(GET_VALUE) TYPE I.

PRIVATE SECTION.

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER IMPLEMENTATION.

METHOD SET_COUNTER.

COUNT = SET_VALUE.

ENDMETHOD.

METHOD INCREMENT_COUNTER.

ADD 1 TO COUNT.

ENDMETHOD.

METHOD GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS.

<b><u>

example to interface</u></b>

INTERFACE I_COUNTER.

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I,

INCREMENT_COUNTER,

GET_COUNTER EXPORTING VALUE(GET_VALUE) TYPE I.

ENDINTERFACE.

CLASS C_COUNTER1 DEFINITION.

PUBLIC SECTION.

INTERFACES I_COUNTER.

PRIVATE SECTION.

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER1 IMPLEMENTATION.

METHOD I_COUNTER~SET_COUNTER.

COUNT = SET_VALUE.

ENDMETHOD.

METHOD I_COUNTER~INCREMENT_COUNTER.

ADD 1 TO COUNT.

ENDMETHOD.

METHOD I_COUNTER~GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS.

CLASS C_COUNTER2 DEFINITION.

PUBLIC SECTION.

INTERFACES I_COUNTER.

PRIVATE SECTION.

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER2 IMPLEMENTATION.

METHOD I_COUNTER~SET_COUNTER.

COUNT = ( SET_VALUE / 10) * 10.

ENDMETHOD.

METHOD I_COUNTER~INCREMENT_COUNTER.

IF COUNT GE 100.

MESSAGE I042(00).

COUNT = 0.

ELSE.

ADD 10 TO COUNT.

ENDIF.

ENDMETHOD.

METHOD I_COUNTER~GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS.

regards,

AshokReddy.

eyal_alsheikh
Active Participant

Hi,

To my opinion, you cannot see a clear difference between interface and class through coding: they both have attributes, methods, events etc.

This two subjects are come from Object Oriented paradigm theory. In OO theory a class is a blueprint of an real world object or a type in your program that represent a real world object. Lets say you are writing a software for a university so you probably will have in your software a Student class, a Course Class, an Employee class etc. On the other hand an interface does not represent an object in the real world. An Interface is a set of operations that have some logical connections that your class must have in order to extend the ability of the object.

that is why interfaces are included in classes.

Example:

You have a class Radio that represent a real world object radio.

Now you want to create a new class type: ClockRadio. A clock radio is a radio with one more feature than a regular radio. Because your ClockRadio can display the hour it must have a set of buttons that a regular Radio does not have in order to manage the hour ability. It must have a SetHour() button and also a display that you can see the hour: GetHour(). Now put the hour and these two operations together and you got your self an Interface:

Interface Clock

Now to create an ClockRadio Class you inherent from Radio class and extends it with the clock feature by using the Clock Interface inside the ClockRadio class definition and implement these two methods in its implementation.

Hope it helps,

Eyal.

0 Kudos

You have to look at classes and interfaces in two ways.

Classes represent the "real object" and do all the work

Interfaces allow you to create a program that will manipulate the class in a predetermined way. Interfaces are the building blocs of programs however they do nothing else but allow you to manipulate the classes in a predetermined way...

How does it happen? If you can cast an object to an interface (we say here the class implements the interface) you are able to manipulate it with the methods of the interface....

Enjoy