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

Abstract Class & Interfaces

Former Member
0 Likes
1,664

Can anyone please tell me as to why we need both an abstract class & an interface? I was asked in an interview as to why we need 2 separate concepts when we can get the similar functionality of an interface by using an abstract class. I had just sited their differences like:

1) An abstract class can have both abstract & normal methods & that we can specify different access specifiers for its class members.

2) ABAP does not support Multiple inheritance but that we could simulate the same using interfaces concept in ABAP.

But he wasnt satisfied with the answer. I guess he was expecting something from a practical point of view. I did try searching the old threads but there wasnt anything similar to this. Anyone please explain by citing a scenario as to why we would need 2 separate concepts & not just one .

Thanks in advance

1 ACCEPTED SOLUTION
Read only

Former Member
1,086

The difference you might have missed is that in case of interfaces, there is no implementation part. So the DEFINITION for INTERFACE is not required.

Moreover some discussions on the OO principles behind Classes and Interfaces might clarify such requirements.

When designing OO applications, sometimes you need to ensure that some Object needs to follow some standardization in terms of behavior.

Example can be birds fly, airplanes fly, mosquitoes also fly. Using abstract class design what you need to do is basically create an abstract class in the name of FLY and attach it some behavior (using methods).

Now create other sub classes (BIRD, AIRPLANE, MOSQUITO). This creates an OO design which says BIRDS IS A FLY, AIRPLANE IS A FLY and MOSQUITO IS A FLY. (Inheritance provides "IS A " kind of relationship in OO) This not only looks odd also asks you to have unwanted relationship.

A better and cleaner approach adopted during later stage of OO development is through interfaces. You can orthogonally attach any interface to a class so you can get "vehicles that can fly as well as move on road" in a better OO way.

3 REPLIES 3
Read only

Former Member
1,087

The difference you might have missed is that in case of interfaces, there is no implementation part. So the DEFINITION for INTERFACE is not required.

Moreover some discussions on the OO principles behind Classes and Interfaces might clarify such requirements.

When designing OO applications, sometimes you need to ensure that some Object needs to follow some standardization in terms of behavior.

Example can be birds fly, airplanes fly, mosquitoes also fly. Using abstract class design what you need to do is basically create an abstract class in the name of FLY and attach it some behavior (using methods).

Now create other sub classes (BIRD, AIRPLANE, MOSQUITO). This creates an OO design which says BIRDS IS A FLY, AIRPLANE IS A FLY and MOSQUITO IS A FLY. (Inheritance provides "IS A " kind of relationship in OO) This not only looks odd also asks you to have unwanted relationship.

A better and cleaner approach adopted during later stage of OO development is through interfaces. You can orthogonally attach any interface to a class so you can get "vehicles that can fly as well as move on road" in a better OO way.

Read only

Former Member
0 Likes
1,086

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.

<b>Interfaces</b>

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.

Read only

0 Likes
1,086

Hi Naresh,

don't forget that ABAP Objects does not support multiple inheritance. Therefore you can only have a single super-class. However a class can implement multiple interfaces.

Cheers

Graham Robbo