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 All,

Can any one give me clear idea on Class & Interface. & methods and events too.

Thanks

Jaya

1 ACCEPTED SOLUTION

sonu_p2
Active Participant
0 Kudos

Hello Jayasri,

Check this out ..................

<b>Class</b>

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 :-

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

&#61558; 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.

&#61558; 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.

<b>Interface</b>

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.

Reward points if helpful.......:-)

Thanks,

Sachin

6 REPLIES 6

Former Member
0 Kudos

Hello

What Is a Class?

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

What Is an Interface?

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it.

[Removed by the moderator.]

Hope this helps,

Dont forget to reward!!

Gabriel P.-

Former Member
0 Kudos

Hi Jaya,

<u><b>Class</b></u>

If a set of real world entities have some common structure and behavior, that can be specified in a template called class. A class is therefore like a blueprint in accordance with which all objects in that class are created.

<u><b>Interfaces</b></u>

Interfaces differ from regular inheritance in their areas of use. There are

hardly any differences in terms of programming.

From a technical perspective, interfaces are simply superclasses that

cannot be instantiated,

do not have an implementation part &

only have public components.

Interfaces primarily serve to define uniform protocols for services.

Various classes implement these services in different ways, keeping the

semantics unaltered. Interfaces contain no implementations.

Use – If multiple classes have to implement a service in different ways, but

using the same method names and a uniform signature, with regular

inheritance such a method (service) will be defined in the superclass.

However if the superclass cannot be modeled suitably for inheritance,

an interface should be defined and method should be defined in the

interface – same as generalization relationship with a superclass.

<u><b>

Methods</b></u>

Methods describe the functions that an object can perform.

They therefore determine the behavior of an object.

<u><b>Events</b></u>

Objects or classes can use events to trigger event handler methods in other

objects or classes. So events are basically a component of a class which when

raised will trigger some event handler method defined for the event.

Refer to following links for more info.

[Removed by the moderator.]

Award points if found useful.

Regards

Indrajit.

sonu_p2
Active Participant
0 Kudos

Hello Jayasri,

Check this out ..................

<b>Class</b>

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 :-

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

&#61558; 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.

&#61558; 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.

<b>Interface</b>

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.

Reward points if helpful.......:-)

Thanks,

Sachin

Former Member
0 Kudos

Hi Jaya,

In layman terms, car is a class and santro wing is an object.

So that means class is a blue print with some properties like 4 wheels, 1 stering, brake,accelerator, etc. Only the properties, no instance, no real

But Santro is a specific car which can be touched and seen and has real meaning.

Similarly, interface is a blue print with some properties, but it cannot have an instance. It can be implemented by any class and the properties of the instance can be used by any object of this class.

Suppose i have a class A with methods a1, a2.

I create an object ob- ob has methods a1 and a2.

There is one interface say Int1 with some methods i1 and i2.

suppose my class A implements interface Int1.

Then i create an object ob- ob will have a1, a2, i1 and i2 methods.

however, we cannot create an instance of interface Int1.

There is one specfic advantage of Interfaces. OOABAP does not support Multiple Inheritance.


class A         class B
                / 
               / 
       class C

one subclass cant inherit from 2 superclasses in ABAP. However multiple inheritance can be still be achieved by using interfaces.

Check out this link... it has very good details.

Hope this helps.

Regards,

Richa

Former Member
0 Kudos

HI Jaya,

In short

class is the full discription of an object.

interface describes the aspect of the object->Data types and functions of intrface can be used by different classes.

Regards

Sarath

Former Member
0 Kudos

hi

good

There is no difference in the functionality of these two.The only difference is that a class cannot extend an abstract class if it already is extending some other class. An interface on the other hand can be implemented in any situation which makes them very powerful.Also user defined exceptions can be defined within an interface itself, which is not the case with an abstract class.

thanks

mrutyun^