SAP-ABAP Interface, Abstract Class Interview Questions Answers, this post consist of commonly asked interview questions on Interfaces and Abstract Classes in OOABAP and difference between Interface and Abstract Class. Refer this post to more about Interface and Abstract Classes in SAP ABAP, and also for Interview Questions on Interfaces & Abstract Class. Click on read more refer this post.
SAP-ABAP-Interface-Abstract-Class-Interview-Questions-Answers
SAP-ABAP Interface, Abstract Class Interview Questions Answers, this post consist of commonly asked interview questions on Interfaces and Abstract Classes in OOABAP and difference between Interface and Abstract Class. Refer this post to more about Interface and Abstract Classes in SAP ABAP, and also for Interview Questions on Interfaces & Abstract Class.
1) What is an Interface?
Ans)
Interfaces are independent structures that you can implement in a
class to extend the scope of that class. Interfaces, like classes, define a set
of properties, methods, and events.
But unlike classes, interface methods do
not provide implementation. They are implemented by classes, and defined as
separate entities from classes.
2) Can we instantiate the interface?
Ans) No, we cannot instantiate an interface.
3) Can we achieve multiple inheritance using Interfaces?
Ans)
Yes, multiple inheritance concept can be achieved using
interfaces. Different interfaces can be implemented in single class.
4) Can we assign values to attributes of interface inside interface?
Ans)
We cannot assign values to attributes of the interface except for
constants. To assign values to interface attributes we need to use DATA VALUES
option in the class while implementing interface.
Example:-
interface inf1.
data w_i type i.
constants w_c type i value 435.
endinterface.
Class zlcl_a definition.
public section.
INTERFACES inf1 data values w_i = 45.
endclass.
5) Does
polymorphism achieved through interfaces?
Ans)
Yes, we can achieve polymorphism using interfaces. Interfaces,
along with inheritance, provide one of the pillars of polymorphism, since they
allow a single method within an interface to behave differently in different
classes.
6) Where can an interface be implemented in the class?
Ans) Interfaces can only be implemented in public section of the class.
7) Is it mandatory to implement all methods of interface in the class
which includes interface?
Ans) Yes, all the methods of the interface need to implemented in the
class.
8) What is the difference between abstract class and interface?
Ans)
INTERFACE
|
ABSTRACT CLASS
|
1) Interfaces
cannot be instantiated.
|
1) Abstract class
also cannot be instantiated
|
2) Interfaces
cannot have implementation.
|
2) Abstract class
can have implementation.
|
3) All the methods
of the interface need to be implemented in the class.
|
3) Only abstract
methods need to be implemented in the subclass.
|
4) No need to use
Redefinition key word while implementing inheritance method in the class.
|
4) Need to use
Redefinition key word while implementing inheritance method in the class.
|
9) What is alias?
Ans)
Aliases are the names for the interface components with which we
cal call the interface methods in order to avoid ambiguity. The method can
directly called using alias names.
Syntax to declare alias name:
ALIASES <alias> FOR intf~comp.
10) Can we make methods of
interface as abstract and final?
Ans)
Yes, we can make methods of
interface as abstract and final using following options.
Syntax:-
INTERFACES intf
{ {[ABSTRACT METHODS meth1 meth2 ... ]
[FINAL METHODS meth1 meth2 ... ]}
| [ALL METHODS {ABSTRACT|FINAL}] }
Note: -
To make interface
methods as abstract methods first we need to make the class which implements
interface as abstract class.
11) Can we do upcast and
downcast using interface references?
Ans) Yes, we can perform casting
on interface references.
Suppose we have a class reference <cref> and interface references
<iref>, <iref1>, and <iref2>. The following assignments with
interface references can be checked statically:
Both interface references must refer to the same interface, or the
interface of <iref1> must contain the interface <iref2> as a
component.
The class of the class reference <cref> must implement the
interface of the interface reference <iref>.
The class of <cref> must be the predefined empty class OBJECT.
In all other cases, you would have to work with the statement MOVE ...?
TO or the casting operator (?=).
The casting operator replaces the assignment operator (=). In the MOVE... ? TO statement,
or when you use the casting operator, there is no static type check.
Instead,
the system checks at runtime
whether the object reference in the source variable points to an object to
which the object reference in the target variable can also point.
If the
assignment is possible, the system makes it, otherwise, the cacheable runtime
error MOVE_CAST_ERROR occurs.
You must always use casting for assigning an interface reference to a
class reference if <cref> does not refer to the predefined empty class
OBJECT:
<cref> ?= <iref>
For the casting to be successful, the object to which <iref>
points must be an object of the same class as the type of the class variable
<cref>.
12) Can we declare events in interface?
Ans) Yes.
13) Can we raise events in interface?
Ans) No.
Dear ABAPers if you know any interview questions related this Interface & Abstract Class topic please share on below comment box.
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.