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: 

Diffrence between a Interface and a abstract class?

Former Member
0 Kudos

Hi OO ABAP Gurus

Please clear below point to me.

Diffrence between a Interface and a abstract class?

Many thanks

Sandeep Sharma..

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Interface:

All the methods of the class ARE UNDEFINED. You need to implement the interface in a class and give the method definition now. In other words INTERFACES just have the declarations no definitions.

Abstract Class:

All the methods which are defined for the class may have the definition or may not. Say we have 3 methods defined for this particular abstract class but only 2 have the definition and 1 does not have the definition. Then it becomes the abstract class.

So, every Interface is a abstract class but not every abstract class is interface.

5 REPLIES 5

Former Member
0 Kudos

Hi

Abstract classes

Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract) subclasses, for example to define a uniform interface.

Classes with at least one abstract method are themselves abstract.

Static methods and constructors cannot be abstract.

You can specify the class of the instance to be created explicitly: CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>.

Abstarct classes themselves can’t be instantiated ( althrough their subclasses can)

Reference to abstract classes can refer to instance of subclass

Abstract (instance) methods are difined in the class , but not implemented

They must be redefined in subclasses

CLASS LC1 DEFINAITION ABSTARCT

PUBLIC SECTION

METHODS ESTIMATE ABSTARCT IMPORTING…

ENDCLASS.

Interfaces

Interfaces only describe the external point of contact of a class (protocols), they do not contain any implementation.

Interfaces are usually defined by a user. The user describes in the interface which services (technical and semantic) it needs in order to carry out a task.

The user never actually knows the providers of these services, but communicates with them through the interface.

In this way the user is protected from actual implementations and can work in the same way with different classes/objects, as long as they provide the services required. This is known as polymorphism with interfaces.

Interfaces features

INTERFACE I_COUNTER.

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I, INCREMENT_COUNTER, 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.

ENDCLASS.

Refer

https://forums.sdn.sap.com/click.jspa?searchID=10535251&messageID=2902116

Regards

Kiran

former_member230674
Contributor
0 Kudos

hai

sandeep sarma,

Interfaces are independent structures that u can implement in a class to extend its scope.The class-specific scope of a class is defined by its components and visibility sections.

Interfaces extend the scope of a class by adding their own components to its public section.Interfaces allowing a single method to behave differently in different classes.They supports both polymorphism and inheritance.

Definition:

INTERFACE <interface1>.

meth1.............

meth2............

ENDINTERFACE.

Implementation:

METHOD <interface1~meth1>.

...

ENDMETHOD.

Abstract Class: means , all methods in a class need not be defined.The first Non-abstract class which extends the abstract

class must implement all the abstract methods in the parenr class.

If it useful, reward points.

GrahamRobbo
Active Contributor
0 Kudos

Hi Sandy,

an interface simply declares attributes and methods - it does not contain any implementation code. Being an interface it can be included in many classes.

An abstract class contains class declaration and implementation - but it cannot be instantiated. Use a subclass to instantiate it.

Cheers

Graham Robbo

Former Member
0 Kudos

Interface

An abstract definition of components for classes that serves as a template for the public interface of classes. Defined using INTERFACE - ENDINTERFACE either globally in an interface pool or locally in another ABAP program. An interface contains the declaration of interface components, but no method implementation. Can be implemented in classes using INTERFACES, hence adding the the interface components to the outer interface. Classes with this type of interface must implement the methods defined in the interface.

Abstract Class

A class that unites characteristics of a generic type for the purpose of reusing these characteristics in different contexts.

An abstract class is not instantiated during the configuration process. Products are not directly assigned to abstract classes.

Regards,

Renjith Michael.

Former Member
0 Kudos

Interface:

All the methods of the class ARE UNDEFINED. You need to implement the interface in a class and give the method definition now. In other words INTERFACES just have the declarations no definitions.

Abstract Class:

All the methods which are defined for the class may have the definition or may not. Say we have 3 methods defined for this particular abstract class but only 2 have the definition and 1 does not have the definition. Then it becomes the abstract class.

So, every Interface is a abstract class but not every abstract class is interface.