OOABAP Training Presentation

87
pyright © 2009 Accenture All Rights Reserved. Object Oriented ABAP

description

ABAP OOPS programming

Transcript of OOABAP Training Presentation

Page 1: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Object Oriented ABAP

Page 2: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Course Contents

Object Oriented ConceptsObject Oriented Programming.Advantages of the Object-Oriented ApproachConcept of Inheritance

Day - 1

Methods• Syntax and Visibility• Instance Methods and Static Methods• Constructor

Reference Variables• Creating References• Assigning References

ClassesComponents of a Class

Attributes• Syntax and Visibility• Instance Attributes and Static Attributes

Page 3: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Course Contents

Day - 2

Inheritance Super classes and Subclasses Visibility Inheritance and the (Instance) Constructor Parameters Redefining Methods in OOABAP Compatibility Principles of the Narrowing Cast Static and Dynamic Components Final Classes and Methods

Page 4: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Day - 2

Course Contents

Polymorphism Advantages Compared to Procedural Programming Abstract Classes and Methods Component Namespaces in Classes

Interfaces Defining and Implementing an Interface Working with Interface Components Interface References

• Narrowing Cast• Widening Cast

Using Several Interfaces Polymorphism and Interfaces Polymorphism and Inheritance Compound Interfaces

Page 5: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Day - 2

Course Contents

Events Define and Trigger Events Handle Events Register and deregister Events Receive a reference from Sender

Page 6: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• ABAP created with intention of improving reporting.• Developed as an in-house programming language influenced by

COBOL and Pascal• Extended to ABAP/4 for ABAP Objects• OOC proved their worth for enterprise application in other

languages were adopted

History of ABAP

Page 7: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Object – Oriented Concepts

What are Objects ?

SAP AG 1999

What Are Objects?

Tree

House

Crane

Objects are an abstraction of the real world

Objects are units made up of data and of the functions belonging to that data

Real worldModel

DataMethodMethod

Method

DataMethodMethod

MethodData

MethodMethodMethod

Boat

DataMethodMethodMethod

Page 8: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Object Oriented Programming

• Encapsulation

• Inheritance

• Polymorphism

• Instantiation

• Interfacing

• Events

OOPS

Page 9: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Complex software systems become easier to understand, since an object-oriented architecture resembles reality more closely than other programming techniques.

• Changes in object-oriented systems should be possible locally (at class level), without further changes being necessary in other parts of the system. This reduces the amount of maintenance required.

Advantages

Page 10: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Polymorphism and inheritance enable many individual components to be reused.

• Object-oriented systems require less adjustment and maintenance, because the majority of problems can be discovered and corrected in the design and development phases.

Advantages

Page 11: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Classes are the central element of object-orientation.

• A Class is an abstract description of an object.

• Classes are templates for objects.

• The attributes of objects are defined by the components of the class, which describe the state and behavior of objects.

Classes

Page 12: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Attributes * Data * Determines state of the object• Methods * Executable code * Determine behavior of the object

Components in a class

Page 13: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Defining Local Classes• A complete class definition consists of a declaration part and an implementation part.

• The declaration part of a class <class> is a statement block: CLASS c1 DEFINITION.   ….  ENDCLASS.

• If you declare methods in the declaration part of a class, you must also write an implementation part for it.

CLASS c1 IMPLEMENTATION. ….   ENDCLASS. 

Classes

Page 14: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Attributes

Page 15: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Defining Local Classes

Classes

Page 16: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Attributes, Types, Constants - Syntax

Page 17: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Attributes and Visibility

Page 18: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Instance attributes and Static attributes

Page 19: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Methods

Page 20: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Methods : Syntax

Page 21: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Methods and Visibility

Page 22: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Instance methods and Static methods

Page 23: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Instance methods and Static methods : Example

Page 24: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Declare a class Private : name type string planetype type saplane-planetype private static attribute : n_o_planetypes type I Public : set_attributes – setting private attributes display_attributes – displaying pvt attributes display_n_o_airplane – display static attributes

Exercise 1

Page 25: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Creating Objects

Page 26: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Define references of the class• Define several instances of the class• Gather all the references into internal table

Exercise 2

Page 27: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Calling method

Calling a method with an instance

CREATE OBJECT plane TYPE lcl_airplane.

CALL METHOD plane->set.

CALL METHOD plane->get IMPORTING get_value = number.

CALL METHOD plane->get EXPORTING get_value = number.

CALL METHOD plane->get CHANGING get_value = number.

Page 28: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Calling a static method

CREATE OBJECT plane TYPE lcl_airplane.

CALL METHOD lcl_airplane=>get_n_o_display.

Calling Method

Page 29: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Page 30: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Call set_attributes by passing valid values• Call display_attributes• Call static method display_n_o_airplanes

Exercise 3

Page 31: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Constructor

Page 32: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Constructor : Example

Page 33: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Static Constructor : Implementation

Page 34: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Static Constructor : Call Examples

Page 35: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Use constructor to count the instances created• Display the count

Exercise 4

Page 36: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Reference Variables

Page 37: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Creating Objects : Syntax

Page 38: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Assigning References

Page 39: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Global classes are individual Repository objects with all of the normal attributes

• Namespace conventions are Y* or Z* is the same as that used for the namespace of other Repository objects.

• Special maintenance tool is available in the Workbench : class builder ( SE 24 )

Global classes

Page 40: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Class Builder

Page 41: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Create a global class with name and planetype as two private attributes

• Write constructor to assign values• Write a method display_attributes to display both the values• Create an instance of this class and call the method with the

created instance

Exercise 5

Page 42: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Inheritance allows you to derive a new class from an existing class. • You do this using the INHERITING FROM addition in the

CLASS <subclass> DEFINITION INHERITING FROM <superclass>

statement.• The new class <subclass> inherits all of the components of the existing class

<superclass>. • The new class is called the subclass of the class from which it is derived. • The original class is called the superclass of the new class.

Inheritance

Page 43: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Inheritance

Page 44: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Inheritance : Syntax

Page 45: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Relationships between super classes and subclasses

Relationships between super classes and subclasses

Page 46: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Inheritance and Visibility

Page 47: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Inheritance and (Instance) constructor

Page 48: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Parameters and CREATE OBJECT

Page 49: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Redefining Methods in ABAP Objects

Page 50: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Redefining Methods : Example

Page 51: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Page 52: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Page 53: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Page 54: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Page 55: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

A use of Aliases

Page 56: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• In class lcl_airplane define subclass lcl_passenger_plane• Attribute of subclass• max_seats type sflight-setsmax• Define constructor which define and implement all instance

attributes• Redefine display_attributes so that all instance attributes are

displayed using write statement

Exercise 6

Page 57: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Define subclass lcl_cargo_plane• Attributes• Private max_cargo type scplane-cargomax• Define and implement constructor that assigns all instance

attributes• Redefine display_attributes to display all attributes• Define get_cargo to return cargo value to calling program. ( use

parameter re_cargo )

Cont …

Page 58: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• In main program • Define suitable type reference variable for each of your new

classes• Call method display_n_o_airplanes ( before instantiating any

objects )• Instantiate reference for lcl_passenger_plane and

lcl_cargo_plane• Call display attributes for both instance• Call static method display_attributes

Cont …

Page 59: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Compatibility and Narrowing Cast

Page 60: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Principles of the Narrowing Cast

Page 61: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Static and Dynamic Types: Example

Page 62: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Static and Dynamic Types for References

Page 63: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Widening the Cast

Static and Dynamic Types for References

Page 64: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Widening the cast

Page 65: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Polymorphism

Page 66: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Polymorphism

Page 67: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Polymorphism

Page 68: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

To understand Polymorphism

• In main program define an internal table which should have type reference to lcl_airplane

• Insert references to your passenger and cargo airplanes into internal table

• Loop through content of internal table and call display_attributes method every time in loop

Exercise 7

Page 69: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Interfaces exclusively describe the external point of contact of a class, but they do not contain their own implementation part.

Interface

Page 70: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Defining and Implementing Interface

Page 71: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Working with Interface components

Page 72: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Interface References Narrowing casting

Page 73: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• The assignment of an object reference to an interface reference is known as a narrowing cast since, as with inheritance, only a part of the object interface is visible once you have assigned the reference.

• With an interface reference, you can no longer address all components in the class carrying out the implementation, but only the components defined in the interface.

Interface

Page 74: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Interface references widening cast

Page 75: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• The widening cast is, as with inheritance, the opposite of the narrowing cast: here it is used to retrieve an object reference from an interface reference. Obviously it cannot be statically checked, since an interface can be implemented by more than one class.

• An object reference cannot be assigned to an interface reference if it has itself not implemented the corresponding interface.

Interface

Page 76: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• In the above example, one class is implementing several interfaces. Even if these interfaces contain components with the same name, they are differentiated in the class carrying out the implementation by the prefix “<interfacename>~”.

Using several Interface

Page 77: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Polymorphism and Interface

Page 78: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Events Overview

Page 79: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Instance events can be triggered by the instances of the class• While developing the class that triggers the event, one need not know about

class handling • At the time of development it is completely unclear what type of handlers there

will be and how many will be used.

Events

Page 80: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

• Objects or Classes use events to trigger Event Handler methods in other objects or classes.

• When an event is triggered any number of Event Handler Methods can be called.

• The events of a class can be raised in the same class using the RAISE EVENT Statement.

• Events have only output parameters which are accepted by the Event Handler Methods as input parameters.

• The link between the trigger and the handler is established dynamically at runtime using the statement SET HANDLER.

Events

Page 81: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Triggering and handling Events : Overview

Page 82: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Defining and Triggering Events

Page 83: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Handling and Registering Events

Page 84: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Handling Events

Page 85: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Registering for an Event : Syntax

Page 86: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Event handling : Characteristics

Page 87: OOABAP Training Presentation

Copyright © 2009 Accenture All Rights Reserved.

Thank You

Happy Coding