OO
ABAP(Object oriented Programming)/Oops ABAP
Features of Oops:
Objects:-
It signifies the real world. Technically we can say objects are instances of a class. We can create any number of objects from a class template. All the objects created has unique identity and each contain different set of attributes. Objects we create in a program exists only till the program exists.
Encapsulation:-
Through encapsulation we restrict the visibility of attributes and methods in the object.
There are three levels of visibility in OO ABAP.
Public
Protected
Private
Visibility at class level:-
Public:-
Features of Oops:
1.Data Abstraction.
2.Encapsulation.
3.Polymorphism.
4.Inheritance.
Class: A Class is a user defined data type which is collection of components.
Class is a prototype that defines data and the behavior common to all the objects of certain kind. Here methods provide the behavior. We can say classes describe objects.
Classes can be declared either globally or locally. Global classes can be declared using transaction SE24. Local classes are declared in a abap program (reports etc).
In ABAP has two types of classes .
Local Class.
Global Class.
Global classes are stored in class pool.
Objects:-
It signifies the real world. Technically we can say objects are instances of a class. We can create any number of objects from a class template. All the objects created has unique identity and each contain different set of attributes. Objects we create in a program exists only till the program exists.
Encapsulation:-
Through encapsulation we restrict the visibility of attributes and methods in the object.
There are three levels of visibility in OO ABAP.
Public
Protected
Private
Polymorphism:-
The name of method is same but they behave differently in different classes. It means implementation of method (i.e. body of the method) is different in different classes. It can be achieved in two different ways in OO ABAP.
1.Interfaces
2. Overriding methods or redefining methods in each class after inheritance.
Inheritance:-
In OO ABAP we use an existing class to derive a new class (child class). The new class contains the attributes of the parent class (derives according to the visibility of attributes and methods) in addition to new attributes and methods added to it. The child class derives all the attributes and methods declared in parent class as public visibility. The child class can not inherit private members. The protected members in parent class are derived in child class but their visibility changes to private.
Interfaces:-
Interfaces are similarly defined as classes. They also contain attributes and methods. But interfaces do not have implementation part. Their methods are implemented in the class that implements the interface.
So we provide different implementation to the methods defined in the interface in different class that implements that interface. This way polymorphism is achieved.
Visibility at class level:-
- Public
- Protected
- Private
Public:-
- By default the visibility of the class is public.
- Public class can be instantiated and can be inherited.
- The sub class inheriting the public class is also created as public by default.
- The sub class inheriting the public class can be created as Explicit Protected or Private Class.
- Protected class cannot be instantiated outside the class .
- Protected class can be inherited .
- The class Inheriting the protected class is also created as Protected by default.
- We cannot create objects for protected class outside the class declaration but we can create it in the sub class method implementation.
- The subclass inheriting the protected class can created explicitly Public class.
- Private class cannot be instantiated outside the class declaration as well as sub in the subclass method implementation.
- The class inheriting the private class is also declared as private by default.
- The subclass inheriting the private class cannot be created as explicit public class. This is possible if the super private class consider as subclass friend.
- This is done by using friend key word at the time of declaring super super private class . in this case the friend class should be forward declared by using Deferred Keyword.
- Deferred Keyword tells SAP that class definition has be delayed and it has been defined some where else in the program.
Consider 2 classes A and B.
A is considering B as friend.
Inside the class B methods we can instantiated class A and access all the components of class A using this object directly irrespective of the Visibility.
No comments:
Post a Comment