Oo abap-sap-1206973306636228-5

69
Hyderabad February 24-26th 2006 Instructor: Subhas Katikala. Object Oriented ABAP

description

 

Transcript of Oo abap-sap-1206973306636228-5

Page 1: Oo abap-sap-1206973306636228-5

Hyderabad February 24-26th 2006

Instructor: Subhas Katikala.

Object Oriented ABAP

Page 2: Oo abap-sap-1206973306636228-5

Workshop Goals

This workshop will enable you to:

• Learn object – oriented concepts.

• Learn the principle of object - oriented programming.

• Learn the application of object

- oriented ABAP.

Page 3: Oo abap-sap-1206973306636228-5

Course Contents

Object Oriented ConceptsObject Oriented Programming.Advantages of the Object-Oriented Approach

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 4: Oo abap-sap-1206973306636228-5

Course Contents

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 5: Oo abap-sap-1206973306636228-5

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 6: Oo abap-sap-1206973306636228-5

Course Contents

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

Page 7: Oo abap-sap-1206973306636228-5

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

DataMethodMethodMethod

DataMethodMethodMethod

DataMethodMethodMethod

Boat

DataMethodMethodMethod

Page 8: Oo abap-sap-1206973306636228-5

Object Oriented Programming

• Encapsulation

• Inheritance

• Polymorphism

• Instantiation

• Interfacing

• Events

OOPS

Page 9: Oo abap-sap-1206973306636228-5

Advantages

• Simplicity

• Explicitness

• Maintainability

• Future Orientation

Page 10: Oo abap-sap-1206973306636228-5

• 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 11: Oo abap-sap-1206973306636228-5

• You define global classes and interfaces 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.

Classes

Page 12: Oo abap-sap-1206973306636228-5

Components in a class

Page 13: Oo abap-sap-1206973306636228-5

Defining Local Classes

• A complete class definition consists of a declaration part and, if required, 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: Oo abap-sap-1206973306636228-5

Attributes

Page 15: Oo abap-sap-1206973306636228-5

Defining Local Classes

Classes

Page 16: Oo abap-sap-1206973306636228-5

Attributes, Types, Constants - Syntax

Page 17: Oo abap-sap-1206973306636228-5

Attributes and Visibility

Page 18: Oo abap-sap-1206973306636228-5

Instance attributes and Static attributes

Page 19: Oo abap-sap-1206973306636228-5

Methods

Page 20: Oo abap-sap-1206973306636228-5

Methods : Syntax

Page 21: Oo abap-sap-1206973306636228-5

Methods and Visibility

Page 22: Oo abap-sap-1206973306636228-5

Instance methods and Static methods

Page 23: Oo abap-sap-1206973306636228-5

Instance methods and Static methods : Example

Page 24: Oo abap-sap-1206973306636228-5

Constructor

Page 25: Oo abap-sap-1206973306636228-5

Constructor : Example

Page 26: Oo abap-sap-1206973306636228-5

Static Constructor : Implementation

Page 27: Oo abap-sap-1206973306636228-5

Static Constructor : Call Examples

Page 28: Oo abap-sap-1206973306636228-5

Creating Objects

Page 29: Oo abap-sap-1206973306636228-5

Reference Variables

Page 30: Oo abap-sap-1206973306636228-5

Creating Objects : Syntax

Page 31: Oo abap-sap-1206973306636228-5

Assigning References

Page 32: Oo abap-sap-1206973306636228-5

• 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 33: Oo abap-sap-1206973306636228-5

Inheritance

Page 34: Oo abap-sap-1206973306636228-5

Inheritance : Syntax

Page 35: Oo abap-sap-1206973306636228-5

Relationships between super classes and subclasses

Relationships between super classes and subclasses

Page 36: Oo abap-sap-1206973306636228-5

Inheritance and Visibility

Page 37: Oo abap-sap-1206973306636228-5

Inheritance and (Instance) constructor

Page 38: Oo abap-sap-1206973306636228-5

Parameters and CREATE OBJECT

Page 39: Oo abap-sap-1206973306636228-5

Redefining Methods in ABAP Objects

Page 40: Oo abap-sap-1206973306636228-5

Redefining Methods : Example

Page 41: Oo abap-sap-1206973306636228-5

Compatibility and Narrowing Cast

Page 42: Oo abap-sap-1206973306636228-5

Principles of the Narrowing Cast

Page 43: Oo abap-sap-1206973306636228-5

Static and Dynamic Types: Example

Page 44: Oo abap-sap-1206973306636228-5

Static and Dynamic Types for References

Page 45: Oo abap-sap-1206973306636228-5

Widening the Cast

Static and Dynamic Types for References

Page 46: Oo abap-sap-1206973306636228-5

Widening the cast

Page 47: Oo abap-sap-1206973306636228-5

Polymorphism

Page 48: Oo abap-sap-1206973306636228-5

Polymorphism

Page 49: Oo abap-sap-1206973306636228-5

Polymorphism

Page 50: Oo abap-sap-1206973306636228-5

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

Interface

Page 51: Oo abap-sap-1206973306636228-5

Defining and Implementing Interface

Page 52: Oo abap-sap-1206973306636228-5

Working with Interface components

Page 53: Oo abap-sap-1206973306636228-5

Interface References Narrowing casting

Page 54: Oo abap-sap-1206973306636228-5

• 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 55: Oo abap-sap-1206973306636228-5

Interface references widening cast

Page 56: Oo abap-sap-1206973306636228-5

• 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 57: Oo abap-sap-1206973306636228-5

• 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 58: Oo abap-sap-1206973306636228-5

Polymorphism and Interface

Page 59: Oo abap-sap-1206973306636228-5

• 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 60: Oo abap-sap-1206973306636228-5

Events Overview

Page 61: Oo abap-sap-1206973306636228-5

Triggering and handling Events : Overview

Page 62: Oo abap-sap-1206973306636228-5

Defining and Triggering Events

Page 63: Oo abap-sap-1206973306636228-5

Handling and Registering Events

Page 64: Oo abap-sap-1206973306636228-5

Handling Events

Page 65: Oo abap-sap-1206973306636228-5

Registering for an Event : Syntax

Page 66: Oo abap-sap-1206973306636228-5

Deregistration

Page 67: Oo abap-sap-1206973306636228-5

Registration/Deregistration : Handler Table

Page 68: Oo abap-sap-1206973306636228-5

Event handling : Characteristics

Page 69: Oo abap-sap-1206973306636228-5

Events and Visibility