Вы находитесь на странице: 1из 87

Copyright 2009 Accenture All Rights Reserved.

Object Oriented ABAP


Copyright 2009 Accenture All Rights Reserved.
Course Contents
Object Oriented Concepts
Object Oriented Programming.
Advantages of the Object-Oriented Approach
Concept of Inheritance

Day - 1
Methods
Syntax and Visibility
Instance Methods and Static Methods
Constructor

Reference Variables
Creating References
Assigning References

Classes
Components of a Class



Attributes
Syntax and Visibility
Instance Attributes and Static Attributes


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


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

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


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
Copyright 2009 Accenture All Rights Reserved.
Object Oriented Concepts
What are Objects ?

Copyright 2009 Accenture All Rights Reserved.
Object Oriented Programming

Encapsulation

Inheritance

Polymorphism

Instantiation

Interfacing

Events

OOPS
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
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
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
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
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
Copyright 2009 Accenture All Rights Reserved.
Attributes
Copyright 2009 Accenture All Rights Reserved.
Defining Local Classes
Classes
Copyright 2009 Accenture All Rights Reserved.
Attributes, Types, Constants - Syntax
Copyright 2009 Accenture All Rights Reserved.
Attributes and Visibility
Copyright 2009 Accenture All Rights Reserved.
Instance attributes and Static attributes
Copyright 2009 Accenture All Rights Reserved.
Methods
Copyright 2009 Accenture All Rights Reserved.
Methods : Syntax
Copyright 2009 Accenture All Rights Reserved.
Methods and Visibility
Copyright 2009 Accenture All Rights Reserved.
Instance methods and Static methods
Copyright 2009 Accenture All Rights Reserved.
Instance methods and Static methods : Example
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
Copyright 2009 Accenture All Rights Reserved.
Creating Objects
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
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.
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
Copyright 2009 Accenture All Rights Reserved.
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
Copyright 2009 Accenture All Rights Reserved.
Constructor
Copyright 2009 Accenture All Rights Reserved.
Constructor : Example
Copyright 2009 Accenture All Rights Reserved.
Static Constructor : Implementation
Copyright 2009 Accenture All Rights Reserved.
Static Constructor : Call Examples
Copyright 2009 Accenture All Rights Reserved.
Use constructor to count the instances created
Display the count
Exercise 4
Copyright 2009 Accenture All Rights Reserved.
Reference Variables
Copyright 2009 Accenture All Rights Reserved.
Creating Objects : Syntax
Copyright 2009 Accenture All Rights Reserved.
Assigning References
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
Copyright 2009 Accenture All Rights Reserved.
Class Builder
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
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
Copyright 2009 Accenture All Rights Reserved.
Inheritance
Copyright 2009 Accenture All Rights Reserved.
Inheritance : Syntax
Copyright 2009 Accenture All Rights Reserved.
Relationships between super classes and subclasses
Relationships between super classes and
subclasses
Copyright 2009 Accenture All Rights Reserved.
Inheritance and Visibility
Copyright 2009 Accenture All Rights Reserved.
Inheritance and (Instance) constructor
Copyright 2009 Accenture All Rights Reserved.
Parameters and CREATE OBJECT
Copyright 2009 Accenture All Rights Reserved.
Redefining Methods in ABAP Objects
Copyright 2009 Accenture All Rights Reserved.
Redefining Methods : Example
Copyright 2009 Accenture All Rights Reserved.
Copyright 2009 Accenture All Rights Reserved.
Copyright 2009 Accenture All Rights Reserved.
Copyright 2009 Accenture All Rights Reserved.
Copyright 2009 Accenture All Rights Reserved.
A use of Aliases
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
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
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
Copyright 2009 Accenture All Rights Reserved.
Compatibility and Narrowing Cast
Copyright 2009 Accenture All Rights Reserved.
Principles of the Narrowing Cast
Copyright 2009 Accenture All Rights Reserved.
Static and Dynamic Types: Example
Copyright 2009 Accenture All Rights Reserved.
Static and Dynamic Types for References
Copyright 2009 Accenture All Rights Reserved.
Widening the Cast
Static and Dynamic Types for References
Copyright 2009 Accenture All Rights Reserved.
Widening the cast
Copyright 2009 Accenture All Rights Reserved.
Polymorphism
Copyright 2009 Accenture All Rights Reserved.
Polymorphism
Copyright 2009 Accenture All Rights Reserved.
Polymorphism
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
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
Copyright 2009 Accenture All Rights Reserved.
Defining and Implementing Interface
Copyright 2009 Accenture All Rights Reserved.
Working with Interface components
Copyright 2009 Accenture All Rights Reserved.
Interface References Narrowing casting
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
Copyright 2009 Accenture All Rights Reserved.
Interface references widening cast
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
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
Copyright 2009 Accenture All Rights Reserved.
Polymorphism and Interface
Copyright 2009 Accenture All Rights Reserved.
Events Overview
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
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
Copyright 2009 Accenture All Rights Reserved.
Triggering and handling Events :
Overview
Copyright 2009 Accenture All Rights Reserved.
Defining and Triggering Events
Copyright 2009 Accenture All Rights Reserved.
Handling and Registering Events
Copyright 2009 Accenture All Rights Reserved.
Handling Events
Copyright 2009 Accenture All Rights Reserved.
Registering for an Event : Syntax
Copyright 2009 Accenture All Rights Reserved.
Event handling : Characteristics
Copyright 2009 Accenture All Rights Reserved.
Thank You

Happy Coding

Вам также может понравиться