41
INSTANTIATION AND OBJECT-ORIENTED 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 1

Embed Size (px)

DESCRIPTION

a

Citation preview

Page 1: Object Oriented Programming Model 1

INSTANTIATION AND OBJECT-ORIENTED 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.

Page 2: Object Oriented Programming Model 1

OBJECT ORIENTED PROGRAMMING MODEL

• ABSTRACTION This refers to the ability to reflect real-world processes as realistically in the

programming language as possible.

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

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

• POLYMORPHISMDifferent 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.

Page 3: Object Oriented Programming Model 1

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.

Page 4: Object Oriented Programming Model 1

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.

Page 5: Object Oriented Programming Model 1

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.

Page 6: Object Oriented Programming Model 1

CLASSES AND OBJECTS

• CLASSGeneral 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.

Page 7: Object Oriented Programming Model 1

LOCAL/GLOBAL CLASSES

• LOCAL CLASSESLocal 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.

Page 8: Object Oriented Programming Model 1

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.

Page 9: Object Oriented Programming Model 1

VISIBILITY SECTIONS

• PUBLICAll 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.

• PROTECTEDThe 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.

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

Page 10: Object Oriented Programming Model 1

COMPONENTS OF THE CLASS• ATTRIBUTES

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

• METHODSThe 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.

• CONSTRUCTORIt 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.

Page 11: Object Oriented Programming Model 1

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

Page 12: Object Oriented Programming Model 1

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.

Page 13: Object Oriented Programming Model 1

Methods 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).

Page 14: Object Oriented Programming Model 1

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.

Page 15: Object Oriented Programming Model 1

INSTANCE AND STATIC METHODS

• STATIC METHODSCan only use Static components in their implementation part.

Can be called using the Class.

• INSTANCE METHODSCan use both static and instance components in their implementation part

Can be called using an instance

Page 16: Object Oriented Programming Model 1

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

Page 17: Object Oriented Programming Model 1

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_airplanesENDCLASS. "c_airplane IMPLEMENTATION

Page 18: Object Oriented Programming Model 1

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( ) .

Page 19: Object Oriented Programming Model 1

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.

Page 20: Object Oriented Programming Model 1

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 object’s status and has no other purpose.

Page 21: Object Oriented Programming Model 1

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.

Page 22: Object Oriented Programming Model 1

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.

Page 23: Object Oriented Programming Model 1

INHERITANCELCL_SUP1 LCL_SUP2

LCL_SUB1 LCL_SUB2 LCL_SUB3

NO MULTIPLE INHERITANCE

Generalization

Specialization

Page 24: Object Oriented Programming Model 1

InheritanceYou 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.

Page 25: Object Oriented Programming Model 1

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.

Page 26: Object Oriented Programming Model 1

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.

Page 27: Object Oriented Programming Model 1

REDEFINING METHODSClass lcl_vehicle DEFINITION.

PUBLIC SECTION.METHODS: estimate_fuel

IMPORTING im_dist TYPE s_distRETURNING 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.

Page 28: Object Oriented Programming Model 1

REDIFINING METHODS• The REDEFINITION statement for the inherited method

must be in the same SECTION as the definition of the original method.(It can’t be in Private section).

• If you redefine a method, you don’t need to enter its interface again in the subclass, but only the name of the method.

• ABAP Objects don’t 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.

Page 29: Object Oriented Programming Model 1

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.

Page 30: Object Oriented Programming Model 1

RULES FOR CALLING THE CONSTRUCTOR

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

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

Page 31: Object Oriented Programming Model 1

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 don’t 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.

Page 32: Object Oriented Programming Model 1

DEFINING AND IMPLEMENTING INTERFACE

• Declaring the interfaceINTERFACE 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 don’t have visibility sections. CLASS lcl_rental IMPLEMENTATION.

METHOD lif_partners~display_partner. … ENDMETHOD. ENDCLASS.

Page 33: Object Oriented Programming Model 1

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 CAN’T BE ABSTRACT.

Page 34: Object Oriented Programming Model 1

FINAL CLASSES• Final Classes can’t have subclasses and can therefore protect

themselves from specialization. Class lcl_truck DEFINITION FINAL

INHERITING FROM lcl_vehicle. …

Endclass.• Final Methods can’t 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.

Page 35: Object Oriented Programming Model 1

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.

Page 36: Object Oriented Programming Model 1

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.

Page 37: Object Oriented Programming Model 1

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.

Page 38: Object Oriented Programming Model 1

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 AttributesENDCLASS. "c_airplane DEFINITION

Page 39: Object Oriented Programming Model 1

Example (2 of 4)• Class ImplementationCLASS 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

Page 40: Object Oriented Programming Model 1

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_airplanesENDCLASS. "c_airplane IMPLEMENTATION

Page 41: Object Oriented Programming Model 1

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( ) .