Вы находитесь на странице: 1из 7

ABAP OOPS:

 Abstract class
 Interface
 Final class
 Inheritance
 Constructor
 Static/Instance
 Persistence class
 Casting

What is OOPS?

 Type of problem-solving method in which the software solution reflects real-world objects.
 Data is hidden and cannot be accessed by external functions.
 Complex software systems become easier to understand.
 OO systems are easier to scale by using the concept of reusability.

Objects:

An object is a section of source code that contains data and provides services. The data forms the
attributes of the object. The services are known as methods (also known as operations or functions).
They form a capsule which combines the character to the respective behavior. Objects should enable
programmers to map a real problem and its proposed software solution on a one-to-one basis.

Classes:

 Central element of object orientation.


 Abstract description of an object.
 Defines state and behavior of objects.

Structure of Class

 Classes contain components.


 Each component is assigned a visibility section.
 Components implement methods.

A Class basically contains the following:-


 Attributes: - Any data, constants, types declared within a class form the attribute of the
class.
 Methods: - Block of code, providing some functionality offered by the class. Can be
compared to function modules.
 Events: - A mechanism set within a class which can help a class to trigger methods of other
class.
 Interfaces:-Interfaces are independent structures that you can implement in a class to
extend the scope of that class.

CONSTRUCTORS:

 Constructors are special methods that are triggered when an object is instantiated from a class.
They are necessary when you want to set the initial state of an object dynamically.
 Like normal methods, there are two types of constructor - instance constructors and static
constructors.

Static Methods

 Static methods are methods which can be called irrespective to the class instance. You can
access only static attributes and static events within the Static method.
 This is how you declare and call static method:

Note: We cannot redefine static methods:


Instance Methods

 Instance methods are methods which can be ONLY called using the object
reference. Instance methods can access instance attributes and instance events.
 This is how you declared and call instance method:

Abstract and final method:

 An abstract method is defined in an abstract class and cannot be implemented in that class.
Instead, it is implemented in a subclass of the class. Abstract classes cannot be instantiated.

 A final method cannot be redefined in a subclass. Final classes cannot have subclasses. They
conclude an inheritance tree.

What is an Abstract Class?


Abstract Class is a special kind of class which can’t be instantiated. We can only instantiate the subclasses
of the Abstract class if they are not abstract. Abstract class should at least contain one abstract method.
Abstract methods are methods without any implementation – only a declaration. We can certainly define
the variables referencing to Abstract class and instantiate with specific subclass at runtime.
What is an Interface?
An interface is not a class. It is an entity which can’t have implementation. An interface can only contain
empty method declaration and components. Interface components are always public. We can later
change the visibility of the components within the implementing class using the ALIASES.

Differences

Since both abstract class and interface are different entity, they have few differences:

 Multiple Inheritance: We can achieve multiple inheritance using Interfaces. Since ABAP
doesn’t support more than one Super class, we can have only one abstract class as Super
class.
 New Functionality: If we add a new method in the Interface, all the implementing
classes have to implement this method. If we don’t implement the method, it would
result into Run-time error. For Abstract class, if we add a non-abstract method, it’s not
required to redefine that in each and every inherited class.

 Default Behavior: We can have a default behavior of a non-abstract method in abstract


class. We can’t have any implementation in Interface as it only contains the empty stub.

 Visibility: All interface components are PUBLIC by default. For Abstract class, we can set
the visibility of each component.

http://zevolving.com/2012/01/abstract-class-vs-interface/
Overriding:

Overriding is useful, when we want to extend the functionality of the inherited method. Static
methods cannot be redefined.

Inheritance

Inheritance allows you to derive a new class from an existing class. You do this using the
INHERITING FROM addition in the

CLASS subclass DEFINITION INHERITING FROM superclass.


The new class subclass inherits all of the components of the existing class superclass. The new
class is called the subclass of the class from which it is derived. The original class is called the
superclass of the new class.

Friends - Friendship between Classes


There is normally a strict division between external (PUBLIC) and internal (PROTECTED and
PRIVATE) classes. A user can only access the public components of a class. This allows you to
change the internal implementation of a class without invalidating its users.

In rare cases, however, classes have to work so closely together that they require access to each
others' protected or private components. The concept of friendship between classes has been
developed so that these components do not need to be made available to all users at the same
time.

Friends

A class can grant friendship to other classes and interfaces (and thus to all classes that
implement this interface). To create this relationship, use the FRIENDS additions of the CLASS ...
DEFINITION statement that includes all classes and interfaces to which you want to grant
friendship. These friends are granted access to the protected and private components of the
class granting the friendship and can always generate instances of this class, independently of
the CREATE addition of the CLASS statement.

Important points:

 To use protected method of a super class we can make a public method in sub class and
then we can use the protected method in subclass.

 Every time a subclass is accessed then the class-constructor of super class is accessed
first then class constructor of sub class.

 Class constructor doesn’t contain any parameter.

 Instance constructor can have only import parameters and exceptions

Вам также может понравиться