You are on page 1of 7

OOPS

Object - Objects have states and behaviors. An object is an instance of a class.


Let us now look deep into what are objects. If we consider the real-world we can find many objects around us, Cars, Dogs, Humans etc. All these objects have a state and behavior. If we consider a dog then its state is . name, breed, color, and the behavior is . barking, wagging, running. If you compare the software object with a real world object, they have very similar characteristics. Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods. So in software development methods operate on the internal state of an object and the object-toobject communication is done via methods. 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.

Class - A class can be defined as a template/ blue print that describe the behaviors/states that object of
its type support. A class is a blue print from which individual objects are created. Sample code of class in java
public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }

A class can have any number of methods to access the value of various kind of methods. In the above example, barking(), hungry() and sleeping() are variables.

Local and Global Classes As mentioned earlier a class is an abstract description of an object. Classes in ABAP Objects can be declared either globally or locally. Global Class: Global classes and interfaces are defined in the Class Builder (Transaction SE24) in the ABAP Workbench. They are stored centrally in class pools in the class library in the R/3 Repository. All of the ABAP programs in an R/3 System can access the global classes Local Class: Local classes are define in an ABAP program (Transaction SE38) and can only be used in the program in which they are defined.

Attributes :- Any data,constants,types declared within a class form the attribute of the class.

Methods - A method is basically a behavior. A class can contain many methods. It is in methods where
the logics are written, data is manipulated and all the actions are executed.

Block of code, providing some functionality offered by the class. Can be compared to function modules. They can access all of the attributes of a class. Methods are defined in the definition part of a class and implement it in the implementation part using the following processing block: METHOD <meth>.
... ENDMETHOD. Methods are called using the CALL METHOD statement.

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.

Instant Variables - Each object has its unique set of instant variables. An object.s state is created by the
values assigned to these instant variables.

A class can contain any of the following variable types.

Local variables . variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.

Inheritance: Inheritance can be defined as the process where one object acquires the properties of
another. With the use of inheritance the information is made manageable in a hierarchical order.

Polymorphism : Polymorphism is the ability of an object to take on many forms. The most
common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed. The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object. A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

Encapsulation : Encapsulation is the technique of making the fields in a class private and
providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Abstraction

: Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class. If a class is abstract and cannot be instantiated, the class does not have much use unless it is sub classed. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Program 1 ( In se38 )
REPORT zak_oop_smpl1.

*----------------------------------------------------------------------* * CLASS class1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS class1 DEFINITION. PUBLIC SECTION. DATA: w_text(40) VALUE 'ABAP object'. METHODS : display. ENDCLASS. "class1 DEFINITION *----------------------------------------------------------------------* * CLASS class1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS class1 IMPLEMENTATION. METHOD display. WRITE:/ 'this is method "DISPLAY"'. ENDMETHOD. "display ENDCLASS. "class1 IMPLEMENTATION START-OF-SELECTION. DATA: aks TYPE REF TO class1."Create a ref. var. with ref. to the class. CREATE OBJECT: aks. WRITE:/ aks->w_text. CALL METHOD: aks->display. "Create a ref. variable with reference to the class

Program 2 ( In se38 )
REPORT zak_oop_smpl2.

*----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION. PUBLIC SECTION. DATA: w_public(30) VALUE 'this is public data.'. METHODS: parentmet. ENDCLASS. "parent DEFINITION *child class definition CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS: childmet. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD parentmet. WRITE /: w_public. ENDMETHOD. "parentmet ENDCLASS. "parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD childmet. SKIP. write /: 'method in class child.', w_public. ENDMETHOD. "childmet ENDCLASS. "child IMPLEMENTATION START-OF-SELECTION. DATA: parent TYPE REF TO parent, child type ref to child.

CREATE OBJECT : parent, child. call METHOD: parent->parentmet, child->childmet.

Program 3 ( In se38 )
REPORT zak_oop_smpl3.

*----------------------------------------------------------------------* * CLASS lcl_employee DEFINITION *----------------------------------------------------------------------* CLASS lcl_employee DEFINITION. PUBLIC SECTION. *-------------------------------------------------------------------*the public section is accesible from outside *-------------------------------------------------------------------TYPES: BEGIN OF t_employee, no TYPE i, name TYPE string, END OF t_employee. METHODS: constructor IMPORTING im_employee_no TYPE i im_employee_name TYPE string, display_employee. *class method are global for all instances CLASS-METHODS: display_no_of_employees. PROTECTED SECTION. *-------------------------------------------------------------------*The protecetd section is accesible from the class and its subclasses *-------------------------------------------------------------------*class method are global for all instances CLASS-DATA: g_no_of_employees TYPE i. PRIVATE SECTION. *-------------------------------------------------------------------*the private section is only accesible from within the class *-------------------------------------------------------------------DATA: g_employee TYPE t_employee. ENDCLASS. "lcl_employee DEFINITION *----------------------------------------------------------------------* * CLASS lcl_employee IMPLEMENTATION *----------------------------------------------------------------------*

CLASS lcl_employee IMPLEMENTATION. METHOD constructor. g_employee-no = im_employee_no. g_employee-name = im_employee_name. g_no_of_employees = g_no_of_employees + 1. ENDMETHOD. METHOD display_employee. write:/ 'Employee', g_employee-no, g_employee-name. ENDMETHOD. METHOD display_no_of_employees. write:/ 'Number of employee is:', g_no_of_employees. ENDMETHOD. ENDCLASS. ********************************************************** *Report *********************************************************** DATA: g_employee1 TYPE REF TO lcl_employee, g_employee2 TYPE REF TO lcl_employee. START-OF-SELECTION. CREATE OBJECT g_employee1 EXPORTING im_employee_no = 1 im_employee_name = 'Ashish Kr'. CREATE OBJECT g_employee2 EXPORTING im_employee_no = 2 im_employee_name = 'abcd'. call METHOD g_employee1->display_employee. call METHOD g_employee2->display_employee. call METHOD g_employee2->display_no_of_employees.