Nothing Special   »   [go: up one dir, main page]

3.sap OOPS-Inhitence, Polymohrishm, Enchapsulation

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 7

SAP ABAP OOPS – Inheritance, Encapsulation, Polymorphism

Inheritance in SAP ABAP

· Inheritance means to derive code functionality from one class to another

· It means defining a class in terms of another (parent) class

· This feature of object orientation promotes reusability of classes

· We can use inheritance while writing a class with which to derive a parent class, by mentioning
the name of parent class in the definition of derived class

· Hence, the class that is inherited is called parent or base class or super class, and the class
inheriting the base class is the child class or derived class or subclass

· Through inheritance, an object from derived class can obtain characteristics of objects from base
class

· The keyword to use for inheritance is ‘INHERITING FROM’, just beside the class definition

SYNTAX FOR DEFINING CLASS:

CLASS <subclass_name> DEFINITION INHERITING FROM <superclass_name>

Report name:ZDATAFLAIR_INHERITANCE
Output:

ABAP Access Control and Inheritance

· We know that a class can be defined as public, private, protected

· So when a subclass inherits a superclass, there are certain rules that govern how the class can
access objects and data of superclass
The following table shows whether a derived class has access to base class, based on whether the base
class is defined as public/protected/private:

ACCESS SAME CLASS DERIVED CLASS OUTSIDE (NON-DERIVED CLASS)

PUBLIC Yes Yes Available

PRIVATE Yes Yes No

PROTECTED Yes No No

The above table is explained as follows –

1. Public Inheritance:

· Public data and members of superclass become public data and members of subclass

· Protected members of superclass become protected members of subclass

· Private members of superclass cannot be accessed by subclass

2. Private Inheritance:

· Public members of superclass become private members of subclass

· Protected members of superclass become private members of subclass

· Private members of superclass cannot be accessed by subclass

3. Protected Inheritance:

· Public members of superclass become protected members of subclass

· Protected members of superclass become protected members of subclass

· Private members of superclass cannot be accessed by subclass

Redefining Methods in Sub Class in SAP ABAP

· We can redefine methods of superclass in subclass

· We usually do this so that we can have subclass-specific methods

· However, we must keep the section of redefinition of the method the same as the parent
method

· We only need to use the name of the inherited method and can access its components using
‘super’ reference
Encapsulation in ABAP

· Encapsulation in object orientation means wrapping data and functions together

· This hides the data and function from the outside world, thus promoting data hiding

· Data hiding means obstructing the view of private data and functions from unwanted
third parties, and data abstraction means showing users only what they need to see

· In ABAP, we can do encapsulation via access methods – public, private, protected.

· We can also perform encapsulation via interfaces (which are similar to classes, the only
difference is that we do not implement anything in an interface, it has to be done via
class inheriting the interface, as shown in example below)

Report name: ZDATAFLAIR_ENHANCEMENT

Output:
Polymorphism in SAP ABAP
· It means using one thing for different operations

· It occurs usually in inheritance – for e.g. redefining methods which we saw under the
concept of inheritance

· Polymorphism means redefining methods to either overload them or override them

· Overloading methods means when we use the same method name but use different
parameters

· Overriding methods means when we use the same method name and parameters, but
the two methods are related via inheritance relationship

Report name:ZDATAFLAIR_POLYMIRPHISM
Output:

You might also like