You are on page 1of 41

INSTANTIATION AND OBJECTORIENTED LANGUAGE

Multiple instances(objects) of the same blue print (type or class) are a fundamental attribute of object-oriented languages The ability to create multiple instances of a class, such as a vehicle, is one of the central attributes of object-oriented languages.

OBJECT ORIENTED PROGRAMMING MODEL


ABSTRACTION
This refers to the ability to reflect real-world processes as realistically in the programming language as possible.

ENCAPSULATION
Implementation details are hidden behind well-defined and documented interfaces. Implementation of the objects are hidden from other components in the system

INHERITENCE
Classes, are derived from existing ones. They inherit all the attributes and methods of the higher-level class and can expand and specialize them.

POLYMORPHISM
Different Objects can present the same interface to the outside. A user only needs to know the interface, by using this, he can access various classes without knowing the details. Objects in different classes react differently to the same messages.

Object Orientation - Advantages


Complex software systems become easier to understand, since object-oriented structuring provides a closer representation of reality than other programming techniques. In a well-designed object-oriented system, it should be possible to implement changes at class level, without having to make alterations at other points in the system. This reduces the overall amount of maintenance. Through polymorphism and inheritance, object-oriented programming allows you to Re-use individual components. The amount of work involved in revising and maintaining the system is reduced, since many problems can be detected and corrected in the design phase.

Classes
Classes describe objects. From a technical point of view, objects are runtime instances of a class. In theory, you can create any number of objects based on a single class. Each instance (object) of a class has a unique identity and its own set of values for its attributes.

Object References
In a program, you identify and address objects using unique object references. Object references allow you to access the attributes and methods of an object.

CLASSES AND OBJECTS


CLASS General description of objects(blueprint).
Specifies status data(attributes) and behavior(methods).

OBJECT(INSTANCE) Representation of real world.


Specific instance of a class. Two different objects can have identical attribute values and still not be identical.

LOCAL/GLOBAL CLASSES
LOCAL CLASSES
Local Classes can be defined within any ABAP program and are only visible there.

GLOBAL CLASSES
Global Classes are created with the Class Builder tool of the ABAP workbench in the Class Library and are visible to any ABAP program. Incidentally, it is unimportant whether you use local or global classes to create objects, except that they differ in their visibility. Objects of local classes can only be created in the same program, whereas objects of global classes can be created in any ABAP program.

CLASS DEFINITION
DECLARATION
CLASS C1 DEFINITION. Public Section. Protected Section. Private Section. ENDCLASS.

IMPLEMENTATION
CLASS C1 IMPLEMENTATION. METHOD M1. ENDMETHOD. METHOD M2. ENDMETHOD. ENDCLASS.

The class statement cannot be nested.

VISIBILITY SECTIONS
PUBLIC
All components which are assigned to this section are public and can be addressed by all users, methods of subclasses and methods of the class itself.They form the external interface of the class.

PROTECTED
The components of this section are protected and can be addressed by the methods of subclasses and the methods of the class itself. They form the interface of the class to its subclasses.

PRIVATE
Components of this section are private and can only be used in the methods of the class itself.

COMPONENTS OF THE CLASS


ATTRIBUTES
Attributes are the data objects within a class. The reflect an Objects state. INSTANCE ATTRIBUTES: DATA statement STATIC ATTRIBUTES: CLASS-DATA statement. A READ-ONLY addition can be used with both DATA stmt.

METHODS
The way in which objects behave is implemented in the methods of a class. INSTANCE METHODS: can access all attributes and events of their own class. STATIC METHODS: can only access static attributes and static events.

CONSTRUCTOR
It is the special method which can be of two types: INSTANCE CONSTRUCTOR: implicitly called for each instantiation of a new object. STATIC CONSTRUCTOR: is called the first time the class is accessed.

EVENTS
All methods of the same class can trigger these events. Other methods of the same or the different class can be declared as special handler methods which react to these events.

ATTRIBUTES, TYPES AND CONSTANTS


CLASS <CLASSNAME> DEFINITION. TYPES: <NORMAL TYPE DEFINITION> CONSTANTS: CONSTANT TYPE <TYPE> VALUE <VALUE>. DATA: VAR1 TYPE <TYPE>, VAR2 TYPE <DDIC_TYPE>, VAR3 TYPE VAR1, VAR4 TYPE <TYPE> VALUE <VALUE>, VAR5 TYPE <TYPE> READ ONLY, VAR6 TYPE REF TO <CLASSNAME>, VAR7 TYPE REF TO <INTERFACE>, CLASS-DATA: ONLY_ONCE TYPE ENDCLASS

Attributes
Instance Attributes: The contents of instance attributes define the instance specific state of an object. You declare them using the DATA statement. Static Attributes: The contents of static attributes define the state of the class that is valid for all instances of the class. Static attributes exist once for each class. You declare them using the CLASS-DATA statement. They are accessible for the entire runtime of the class. All of the objects in a class can access its static attributes. If you change a static attribute in an object, the change is visible in all other objects in the class.

Instance Methods You declare instance methods using the METHODS statement. They can access all of the attributes of a class, and can trigger all of the events of the class. Static Methods You declare static methods using the CLASS-METHODS statement. They can only access static attributes and trigger static events. Special Methods As well as normal methods, which you call using CALL METHOD, there are two special methods called CONSTRUCTOR and CLASS_CONSTRUCTOR, which are automatically called when you create an object (CONSTRUCTOR) or when you first access the components of a class (CLASS_CONSTRUCTOR).

Methods

METHODS
PARAMETER INTERFACE
The parameter interface of a method is defined by the additions to the METHODS statement in the declaration part of the class. The implementation part does not require any more details for the parameter interface. the complete syntax for defining the parameter interface is:

METHODS meth [ IMPORTING <im_var> TYPE <type> EXPORTING <ex_var> TYPE <type> CHANGING <ch_var> TYPE <type> RETURNING VALUE <re_var> TYPE <type> RAISING <class_exception> ]. All input parameters(IMPORTING, CHANGING parameters) can be defined as OPTIOAL or DEFAULT.

INSTANCE AND STATIC METHODS


INSTANCE METHODS Can use both static and instance components in their implementation part
Can be called using an instance

STATIC METHODS Can only use Static components in their implementation part.
Can be called using the Class.

Class Example (1 of 3)
(Defining the class)
CLASS c_airplane DEFINITION. * defining the access specifiers. PUBLIC SECTION. CONSTANTS: pos_1 TYPE i VALUE 30. * parameterized method METHODS: set_attributes IMPORTING im_name TYPE string im_planetype TYPE string. * method METHODS: display_attributes. * static method. CLASS-METHODS: display_n_o_airplanes. PRIVATE SECTION. DATA: name TYPE string, planetype TYPE string. * static variable. CLASS-DATA: n_o_airplanes TYPE i. ENDCLASS. "c_airplane DEFINITION

Class Example (2 of 3)
(Implementing the class)
Type-pools icon. CLASS c_airplane IMPLEMENTATION. * presenting the logic the methods. METHOD set_attributes. name = im_name. planetype = im_planetype. n_o_airplanes = n_o_airplanes + 1. ENDMETHOD. "set_attributes METHOD display_attributes. WRITE: / icon_ws_plane AS ICON, / 'Name of Airline: '(001), AT pos_1 name, / 'Airplane Type'(002), AT pos_1 planetype. ENDMETHOD. "display_attributes METHOD display_n_o_airplanes. WRITE: / 'Total Number of planes ', AT pos_1 n_o_airplanes LEFT-JUSTIFIED. ENDMETHOD. "display_n_o_airplanes ENDCLASS. "c_airplane IMPLEMENTATION

Class Example (3 of 3)
(Using the class)
data: plane_name type string, plane_type type string. data: obj_air_plane type ref to c_airplane. defining the object. start-of-selection. create object obj_air_plane. creating the object.

plane_name = 'JET AIRWAYS'. plane_type = 'BOEING 747'. * calling the methods using the instance object. obj_air_plane->set_attributes( im_name = plane_name im_planetype = plane_type ) . * calling the methods using the instance object. obj_air_plane->display_attributes( ) . * calling the class name --> static method.. c_airplane=>display_n_o_airplanes( ) .

CONSTRUCTOR
A Constructor is a special method which is automatically executed by the runtime environment. Constructors are used to set an object dynamically to a defined initial state. A constructor cannot be called with the CALL METHOD statement , nor can the developer influence the call point. Like other methods constructors can be instance dependent or instance-independent. It must be declared in the PUBLIC section of the class declaration.

INSTANCE CONSTRUCTOR
Instance Constructors are called once for each instance of a class after the complete creation of the instance with the CREATE OBJECT statement. It must be declared in the definition part of the class. It must have the predefined name CONSTRUCTOR METHODS CONSTRUCTOR IMPORTING {VALUE (i1)|i2} {TYPE type| LIKE dobj} [OPTIONAL|DEFAULT defi ] EXCEPTIONS xi The fact that the constructor does not have the output parameters shows that it is used solely to define an objects status and has no other purpose.

STATIC CONSTRUCTOR
The Static Constructor is called in a program once for each class before the class is accessed for the first time. The name of the Static Constructor with which it is declared and implemented is class_constructor. CLASS-METHODS class_constructor No interface parameters can be defined for a static constructor. Like all other static methods, static constructors can only access static attributes of their class.

CREATING OBJECTS
Objects are created using CREATE OBJECT statement. Objects can only be created and addressed using reference variables. DATA: <obj1> TYPE REF TO <class1> CREATE OBJECT <obj1> CREATE BOJECT statement creates an object in the Main memory. The attribute values of this of this object are either initial values or correspond to the VALUE entry.

INHERITANCE
Generalization
LCL_SUP1 LCL_SUP2

NO MULTIPLE INHERITANCE
LCL_SUB1 LCL_SUB2 LCL_SUB3

Specialization

Inheritance
You can use an existing class to derive a new class. Derived classes inherit the data and methods of the Super classes. However, they can redefine existing methods, and also add new ones.

RELATIONSHIP BETWEEN SUPERCLASS AND SUBCLASS


Common Components only exist once in the superclass. New components in the superclass are automatically available in subclasses. Amount of new coding is reduced
Subclasses are extremely dependent on superclasses White Box Reuse: Subclass must possess detailed knowledge of the implementation of the superclass.

INHERITANCE
Class lcl_veh Definition
Public Section. Private Section. Endclass.

Class lcl_truck Definition INHERITING FROM lcl_veh Public Section. Private Section. Endclass.

All Components from the superclass are automatically present in the subclass.

REDEFINING METHODS
Class lcl_vehicle DEFINITION. PUBLIC SECTION. METHODS: estimate_fuel IMPORTING im_dist TYPE s_dist RETURNING VALUE (re_fuel) ty_fuel Endclass. Class lcl_truck DEFINITION INHERITANCE FROM lcl_vehicle PUBLIC SECTION. METHODS: estimate_fuel REDEFINITION. Endclass. Class lcl_truck IMPLEMANTATION. METHOD estimate_fuel. Super -> estimate_fuel() ENDMETHOD. Endclass.

REDIFINING METHODS
The REDEFINITION statement for the inherited method must be in the same SECTION as the definition of the original method.(It cant be in Private section). If you redefine a method, you dont need to enter its interface again in the subclass, but only the name of the method. ABAP Objects dont support Overloading. Overloading is possible with the constructor. With the redefined method, you can access the components of the direct Superclass using the SUPER reference.

INHERITANCE AND REDEFINING THE CONSTRUCTOR


The Constructor of the superclass must be called within the constructor of the subclass(to ensure that the objects are initialized correctly). The Static Constructor in the super class is called automatically. The runtime system ensures that the static constructor of all its superclasses have already been executed before the static constructor in a particular class is executed.

RULES FOR CALLING THE CONSTRUCTOR


CASE 1 Class of instance to be created has constructor.
Fill its parameters

CASE 2 Class of instance to be created has no constructor. => Search for the next superclass with a constructor in the inheritance tree. => Fill its parameters.

INTERFACES
In ABAP objects, interfaces are implemented in addition to and independently of classes. Interfaces only describe the external point of contact of a class, they dont contain any implementation. Features: Simulation of Multiple inheritance Black Box Principle: client only knows the interface, not the implementation. Interface as a generalization of the implementing class.

DEFINING AND IMPLEMENTING INTERFACE


Declaring the interface
INTERFACE lif_partners. METHODS: display_partner. ENDINTERFACE.

The Server classes make the interface public.


CLASS lcl_rental DEFINITION. PUBLIC SECTION. INTERFACES lif_partners. ENDCLASS.

Interface are implemented in classes. Interface dont have visibility sections. CLASS lcl_rental IMPLEMENTATION. METHOD lif_partners~display_partner. ENDMETHOD. ENDCLASS.

ABSTRACT CLASSES
Abstract classes themselves cannot be instantiated. Although their subclasses can. Abstract(instance) methods are defined in the class, but not implemented. They must be redefined in subclasses. Classes with at least one abstract method are abstract.
STATIC METHODS AND CONSTRUCTORS CANT BE ABSTRACT.

FINAL CLASSES
Final Classes cant have subclasses and can therefore protect themselves from specialization. Class lcl_truck DEFINITION FINAL INHERITING FROM lcl_vehicle. Endclass. Final Methods cant be redefined in subclasses and can protect itself in this way against uncontrolled redefinition. Class lcl_bus DEFINITION FINAL INHERITING FROM lcl_vehicle. PUBLIC SECTION. METHOD estimate_number_of_seats FINAL. Endclass.

SUMMARY
A final class implicitly only contains final methods. In this case you cannot enter FINAL explicitly in these methods. Methods cannot be both FINAL and ABSTRACT. Classes, on the other hand, that are both ABSTRACT and FINAL can be useful.

FRIENDS CONCEPT
In rare cases, Classes have to work together closely and make all their components, including the private and protected ones, available to each other. Efficient direct access to the data of a class providing friendship. Methods that access the same data can therefore be spread over several classes. Friendship is ONE-SIDED: A class providing friendship is not automatically a friend of its friend. Class lcl_truck DEFINITION FRIENDS lcl_bus. Endclass. All the subclasses of class lcl_bus have access to data of lcl_truck, the other way is not possible.

Reference Variable ME
Within a class, you can use the self-reference ME to access the individual components:
To access an attribute in the same class ME -> <attr> To call a method in the same class CALL METHOD ME -> <meth>

Self references allow an object to give other objects a reference to it.

Example (1 of 4)
Class Definition. CLASS cl_airplane DEFINITION. PUBLIC SECTION. CONSTANTS: pos_1 TYPE i VALUE 30. METHODS: set_attributes IMPORTING " Instance Methods im_name TYPE string im_planetype TYPE saplane-planetype, display_attributes. CLASS-METHODS: display_n_o_airplanes. " Static Methods PRIVATE SECTION. DATA : name TYPE string, planetype TYPE saplane-planetype. " Instance Attributes CLASS-DATA: n_o_airplanes TYPE i. " Static Attributes ENDCLASS. "c_airplane DEFINITION

Example (2 of 4)
Class Implementation CLASS cl_airplane IMPLEMENTATION. METHOD set_attributes. DATA: name TYPE string VALUE 'AIRBUS', planetype TYPE saplane-planetype VALUE 'A-320'. IF im_name IS INITIAL AND im_planetype IS INITIAL. name = name. me->planetype = planetype. me->n_o_airplanes = me->n_o_airplanes + 1. ELSE. me->name = im_name. me->planetype = im_planetype. n_o_airplanes = n_o_airplanes + 1. ENDIF. ENDMETHOD. "set_attributes

Example (3 of 4)
METHOD display_attributes. WRITE : / 'Name of airplane:'(001), AT pos_1 name, / 'Airplane type: '(002), AT pos_1 planetype. ENDMETHOD. "display_attributes

METHOD display_n_o_airplanes. WRITE: /,/ 'Total number of planes'(cal), AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /. ENDMETHOD. "display_n_o_airplanes ENDCLASS. "c_airplane IMPLEMENTATION

Example (4 of 4)
Creating class reference and accessing the class attributes & methods
DATA: r_plane TYPE REF TO cl_airplane. " creating the class reference
START-OF-SELECTION. CREATE OBJECT r_plane. CALL METHOD r_plane -> set_attributes( im_name = 'BOEING' im_planetype = '747-200' ).

CALL METHOD r_plane -> display_attributes( ).


CALL METHOD cl_airplane => display_n_o_airplanes( ) .

You might also like