Introduction Into Object Orientation - Oops Introduction Into Object Orientation
Introduction Into Object Orientation - Oops Introduction Into Object Orientation
Contents
1 Introduction
1.1 Purpose
2 The Object-Orientation Concept
2.1 Observation
2.2 Focus
2.3 Basic Terminology
2.3.1 Classes
2.3.2 Objects / Instances
2.3.3 Attributes
2.3.4 Methods
2.3.5 Construktor
2.3.6 Object Identity and Reference Semantics
2.3.7 Visibility
2.3.8 Events
2.4 Inheritance
2.4.1 Polymorphism
2.4.2 Generalization and Spezialization
2.5 Interfaces
3 Softwareengineering – the OO Development Process
3.1 OO Engineering aspects
3.1.1 Responsibilities
3.1.2 UML - The modeling Language
3.1.3 Architecture
3.2 The iterative Development Process
3.3 Analysis
3.4 Design
3.4.1 Design Patterns
3.5 Implementation
3.5.1 Client-Server
3.5.2 Responsibilities
4 Advantages and Disadvantages
4.1 Quality
4.1.1 Abstraction and Encapsulation
4.1.2 Inheritance and Polymorphism
4.1.3 Reusability
Page 1 of 36
Introduction into Object Orientation - OOPs
4.1.4 Componentware
4.1.5 Interfaces
5 Conclusion
6 Reading List
6.1 Literaturverzeichnis
1 Introduction
1.1 Purpose
2.2 Focus
Page 2 of 36
Introduction into Object Orientation - OOPs
2.3.1 Classes
A Class describes a general element or a general concept, for example the abstract
concepts Business Partner, Material, Transaction, Equipment or List. Classes realize an
abstract data type.
Classes contain components: Attributes, Methods and Events. These
components are described later in this document.
Page 3 of 36
Introduction into Object Orientation - OOPs
Object – A person, place, thing, concept, or event that is applicable to the system at hand.
Objects both know things (i.e., they have data) and they do things (i.e. they have
functionality).
In ABAP Objects, objects are created with the command CREATE OBJECT. The
developer receives an object reference as a result of its creation:
Classes are more or less the blueprint, objects are the realization of
the blueprint. Classes define the structure and behavior of all objects,
which belong to them.
Observation
Before I present the individual components of a Class, I will briefly
mention some common areas of confusion:
confusion between software objects and real-world objects
and
confusion between objects and Classes
Page 4 of 36
Introduction into Object Orientation - OOPs
2.3.3 Attributes
In one of the above sections I mentioned the objects Meyer and Miller,
which are both instances of the Class Business Partner. You may have
asked yourself the question: How does an object know, that it
represents the person Meyer or Miller?
This is the role of Attributes. Attributes can take on values within an
object at runtime. The sum of all attributes and their values describes
the state of an object.
In ABAP Objects attributes are defined in the definition part via the ABAP keyword
DATA. In this example we have defined that the name of a business partner may be 50
characters long and is of the data type Character:
CLASS cl_party DEFINITION.
PUBLIC SECTION.
"I will return later to the meaning of the language element Public Section see Visibility
Page 5 of 36
Introduction into Object Orientation - OOPs
PUBLIC SECTION.
CLASS-DATA: instance_count type i.
DATA: name(50) type c.
ENDCLASS.
START-OF-SELECTION.
DATA: instance TYPE REF TO cl_party.
* Access to the different attribute types is also defined in the syntax:
* Class attribute: cl_class=>instance_count
* Instance attribute: instance->name
Attributes therefore describe the structure (type) of the Class and all of
the objects belonging to the Class. Note that the attribute values do
not guarantee the uniqueness of objects. During a program run many
objects with the name Miller could be created. These objects would all
have - as in real-life - a separate identity and are different business
partners who happen to have the same name.
2.3.4 Methods
ABAP Objects differentiate between instance-dependent and class-dependent methods via the
Page 6 of 36
Introduction into Object Orientation - OOPs
START-OF-SELECTION.
DATA: instance TYPE REF TO cl_party.
DATA: bool TYPE Boolean.
2.3.5 Construktor
Page 7 of 36
Introduction into Object Orientation - OOPs
START-OF-SELECTION.
DATA: instance TYPE REF TO cl_party.
* At this point the class constructor and the instance constructor are executed
CREATE OBJECT instance EXPORTING im_name = 'Miller'.
Page 8 of 36
Introduction into Object Orientation - OOPs
With the help of the previous examples, you have established that
objects belonging to a class are not created by the simple definition of
the class. Neither does the instruction DATA: instance ref to
cl_party.create an object. This instruction only creates a Reference,
which in its initial state has the logical value INITIAL . Only with the
instruction CREATE OBJECT instance is the memory area for a new
object requested from the system. The reference instance then refers
to the object which has just been created. (The command CLEAR
instance. at this point means that the object, to which the reference
variable refers, cannot be referenced. Therefore it can no longer be
addressed in this progam run. A Garbage Collector running in the
background ensures that the object is removed from memory.
This separates object-oriented implementation from classic
implementation. With the classic DATA instruction, main memory is
reserved (which might never be used) and is preallocated the initial
state of the relevant variable. With the "object-oriented" instruction
DATA-x-TYPE-REF-TO, only the intention to create an object is
expressed. The only storage space occupied is for an object reference.
In addition, every object has its own identity. Two references, which
refer to objects, are only identical if they refer to the same object.
Similarity between the attribute values of these objects is not the
deciding factor.
I want to explain in more detail the terms object identity and reference
semantics using the following example.
Page 9 of 36
Introduction into Object Orientation - OOPs
ENDCLASS.
START-OF-SELECTION.
CREATE OBJECT instance EXPORTING im_name = 'Miller'.
CREATE OBJECT instance2 EXPORTING im_name = 'Miller'.
Instance3 = instance.
IF instance3 = instance.
* Both references refer to the same object and are therefore
identical.
ENDIF.
2.3.7 Visibility
Page 10 of 36
Introduction into Object Orientation - OOPs
guarantees its user specific properties and specific behavior. The sum
of these properties is called the class interface. The Visibility
mechanism defines the class interface which is available to the users.
There are three commonly defined types of visibility in object-oriented
technology:
Public
The relevant class component (attribute, method, event) is visible to all classes.
Protected
The relevant class component (attribute, method, event) is visible to the class
itself and all inheritors. We will return to the terms Inheritor and Inheritance later in this
document (see Inheritance). What is important here is the difference between Public and
Private.
Private
The relevant class component (attribute, method, event) is only visible to the class
itself.
You may be saying to yourself: "Now I know what is meant by the term PUBLIC
SECTION." All class components defined in the PUBLIC SECTION are callable by
their user, all others are not (with the exception of the PROTECTED SECTION). You can
use this construct to implement Information Hiding.
In this example the attribute name is hidden from the users. Access to the information it
contains is realized via the method get_name.
Page 11 of 36
Introduction into Object Orientation - OOPs
START-OF-SELECTION.
CREATE OBJECT instance EXPORTING im_name = 'Miller'.
CALL METHOD instance->get_name IMPORTING ex_name = name.
Observation
In this example I will demonstrate that with the help of visibility and
the consequent definition of an independent interface, the individual
(private) implementation of a class can be separated from its users.
While the user knows the format of the business partner name as a
global type PName (50 character text field), the class itself implements
the name as a 75 character text field.
2.3.8 Events
Instances of the Class cl_class create an event object_created. The class method
process_event belonging to the class cl_class1 processes this event.
Page 12 of 36
Introduction into Object Orientation - OOPs
ENDMETHOD.
ENDCLASS.
With the help of the class method initialize the class cl_class1
registers the method process_event at the event object_created for
all instances of the class cl_class. When an object is created for the
class cl_class (CREATE OBJECT instance.), the event
object_created is raised in the constructor. The consequence is that
the method process_event belonging to the class cl_class1 is
executed.
2.4 Inheritance
Inheritance defines the relationship between classes, in which a class (subclass) uses the
structure and behavior that has already been defined in one or more other classes
(superclasses).
Page 13 of 36
Introduction into Object Orientation - OOPs
You may be thinking: "So I have many types of collection. This is therefore a good
opportunity to implement my new knowledge. I will implement each type of collection as
a class!" In principle this approach is correct. However, you will soon establish that all
collections have several components in common:
Each class requires a method in order to add objects to a collection
Each class requires a method in order to delete objects from a collection
Each class has a method which identifies the number of object references in the
collection
... You can probably think of some other similarities
Inheritance is the solution to this situation. You implement all of the similarities in the
class Collection. You then implement the individual types of collection in their own
classes which are Subclasses of the class Collection. As a subclass, these classes
inherit all of the components of the Superclass. Attributes, methods and events are
inherited. In addition, you can implement additional attributes, methods and events in the
subclass. If a method from the Public Section or Protected Section of the
superclass cannot be used in this way, then the method can be redefined in the subclass.
You can find further information in the following sections: Polymorphism und
Generalization and Specialization.
Page 14 of 36
Introduction into Object Orientation - OOPs
Page 15 of 36
Introduction into Object Orientation - OOPs
ENDCLASS.
In the method add belonging to the cl_set the method add belonging
to the superclass cl_collection is called via CALL METHOD SUPER-
>add.
Observation
In order to avoid a class becoming a superclass, FINAL is added. This
addition says that no subclass may be defined for the relevant class. In
order to avoid the situation where a class carries instances, there is the
addition ABSTRACT. Classes which carry this addition, should generally
serve as a superclass and contain a default implementation. This
approach is particularly meaningful in frameworks.
2.4.1 Polymorphism
The first scenario will not occur very often in ABAP Objects, as the interface concept
was created precisely for such cases (see Interfaces).
Page 16 of 36
Introduction into Object Orientation - OOPs
that all animals have in common, but not all plants. On the other hand,
there are characteristics that all plants have in common, which do not
apply for all animals.
This principle of hierarchical classification, which lies at the heart of
human thought, is represented in object-oriented development by the
inheritance concept. Classes are therefore subdivided into subclasses.
These subclasses are defined as a Specialization of the superclass.
Superclasses are then defined as a generalization of the subclass.
2.5 Interfaces
The Interface concept describes a class interface. You can define the
same components in an interface that you can in classes, however
without implementing them.
Classes can implement interfaces, and subsequently be addressed via
these interfaces. For example, a class cl_class can implement the
interface if_archive and thereby become archivable, or it can
implement if_workflow and thereby take part in a workflow. This
opens an additional mechanism of polymorphism, however without
being dependent on inheritance.This combination of classes with simple
inheritance and interfaces is more highly regarded by many experts than full multiple
inheritance. Example: Java.
In addition to object references (DATA: instance TYPE REF TO
cl_class) there are also Interface References (DATA: reference TYPE
REF TO cl_interface). A class which implements a specific interface
can be addressed via this interface reference. Using such an interface
reference, you can access the components defined in the interface. In
this way a user can view different classes through the 'spectacles' of
an interface and address them in a uniform manner.
Interfaces therefore define specific, generic functionality. In contrast to
classes, interfaces generally have not only many users, but also
many implementers.
The Interface if_interface is implemented via the classes cl_class und cl_class1.
INTERFACE if_interface.
METHODS: a_method.
ENDINTERFACE.
Page 17 of 36
Introduction into Object Orientation - OOPs
...
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
CREATE OBJECT instance.
ifref = instance. "This assignation is described as cast
CALL METHOD ifref->a_method.
Page 18 of 36
Introduction into Object Orientation - OOPs
3.1.1 Responsibilities
This should be the central question for every developer. For each
functionality that is required, you should clarify in which area of
responsibility of a class/object the required functionality lies. This class
should then also offer a relevant service (method, event etc). If the
responsibility lies outwith your own area, then corresponding
communication is required. The individual developers may have to talk
with each other, in order to decide how the functional responsibility
should be allocated.
Page 19 of 36
Introduction into Object Orientation - OOPs
Page 20 of 36
Introduction into Object Orientation - OOPs
Activity Diagram
Diagram to represent system behavior (particularly suitable for workflow and
parallel processes).
Collaboration Diagram
Diagram of static and dynamic representation of communications relationships
between classes or objects.
Object Diagram
This diagram models relationships between objects, which are not directly visible
in the class diagram
Component Diagram
Diagram representing development components and their dependencies
Deployment Diagram
Diagram representing hardware and software dependencies
I can only briefly explain the UML in this section. The next picture
shows a class diagram. If you want more information on the UML, we
recommend the book: "UML Distilled – Applying the Standard Object
Modeling Language" by Martin Fowler.
3.1.3 Architecture
Page 21 of 36
Introduction into Object Orientation - OOPs
If this is not
the concept for representing objects, error handling and much more.
the case, then it is harder to get classes to communicate with one
another. It is particularly difficult to integrate different classes into a
functioning software package.
In classic programs, data is passed from interface to interface. Where a
program has to use two non-matching interfaces, a conversion routine
can easily be inserted and you can carry on.
This is no longer possible in object-oriented systems. The calling class
no longer has the authority to transfer data between two interfaces. It
only has references to objects, whose data it does not know. Data
conversion by the calling class is no longer possible.
Bertrand Meyer considers the term "architecture" to be so important in
the object-oriented environment, that he places the Architecture
Principle first in his list of the ten fundamental concepts of object
technology:
Page 22 of 36
Introduction into Object Orientation - OOPs
After validation (e.g. after the first increment, you can see that the project aim is
achievable), the next increment can be carried out for the next most important
project part.
3.3 Analysis
3.4 Design
In the book by Erich Gamma et al. (Erich Gamma, Richard Helm, Ralph
Johnson, John Vlissides: Design Patterns - Elements of Reusable Object-oriented
Software, Addison-Wesley, ISBN: 0-201-63361-2), the authors write:
... and that is also my opinion. But don't worry, the design has not
necessarily become more difficult. For classically designed software,
many modeling aspects are first decided in the implementation phase,
without the participants noticing. Therefore many problems are simply
not visible at the design phase.
The sentence: "We'll make the decision at implementation when we
know this or that" is often heard during the modeling phase, when
decisions are being avoided.
However, these (postponed) decisions must, for the object-oriented
procedure model, be decided during the modeling phase. An
incomplete class model has a significantly greater negative effect on
the implementation. Where there is an obvious gap in the design, an
implementer cannot quickly "program something in", but must first
interrupt his or her work and consider what needs to be done.
Page 23 of 36
Introduction into Object Orientation - OOPs
Each pattern describes a problem which occurs over and over again in our environment,
and then describes the core of the solution to that problem, in such a way you can use this
solution a million times over, without ever doing it the same way twice.
Christoper Alexander, Sara Ishikawa, Murray Silverstein, Max Jacobson, Ingrid Fiksdahl-king, Shlomo
Angel: A Pattern Language, Oxford University Press, New York, 1977
3.5 Implementation
Page 24 of 36
Introduction into Object Orientation - OOPs
3.5.1 Client-Server
3.5.2 Responsibilities
This should also be the central question here for every developer.
If a class uses services from another class, then this client is naturally responsible for
adhering to the given protocol prescribed by the server. Therefore the client should
behave in a server-conformist manner. This demand on the client naturally contains an
implicit demand on the server class: the server class must publish the context in which it
may be used, in a manner that is understandable by the clients (in other words, classes
must be well documented). Server classes should define their interfaces in such a way
that potential clients can fill the context. An internal context request within the server
class is always useful. Bertrand Meyer embedded a possible solution to this requirement
in the programming language Eiffel: methods can be defined using preconditions,
postconditions and variants.
Page 25 of 36
Introduction into Object Orientation - OOPs
4.1 Quality
According to Bertrand Meyer, the following factors benefit to a lesser extent from object
orientation, but they should still be named for the sake of completeness:
Efficiency is the ability of a software system to place as few demands as possible
on hardware resources, such as processor time, space occupied in internal and
external memories, bandwith used in communication devices.
Portability is the ease of transferring software products to various hardware and
software environments.
Verifiability is the ease with which inspection procedures can be created, in
particular test data and procedures for error trapping and handling.
Integrity is the ability of a software system to protect its software elements from
unauthorized accesses and changes.
Ease-of-use is the ease with which people of various backgrounds and
qualifications can learn to use software products and apply them to solve
problems. It also covers the ease of installation, operation and monitoring.
These themes naturally need to be backed-up. I will briefly do this in the following
sections. For a complete list of arguments, see the books listed below in the reading list.
Page 27 of 36
Introduction into Object Orientation - OOPs
advantage is that the new class does not have to appear at all in an
inheritance tree. The implementation in the interface is sufficient.
4.1.3 Reusability
4.1.4 Componentware
4.1.5 Interfaces
Page 28 of 36
Introduction into Object Orientation - OOPs
5 Conclusion
This introduction to object-orientation is not a complete explanation of
the subject. It is only intended to give you a brief insight into object-
orientation. I hope I have inspired you to learn more. If this is the case,
then I can recommend several books. These books should help to give
you a deeper insight into the material.
Before you lay aside this introduction, I would like to give you one more
piece of advice:
Remember that you did not learn everything you know now in a few
days. Experience has shown that high project expectations are
generally not fully realized in the first project - unless you have project
team members who are experienced in object-orientation.
6 Appendix
6.1 Reading List
ABAP/4 and ABAP Objects are registed trademarks of SAP AG, Java is registed
trademark of SUN Microsystems
Page 29 of 36
Introduction into Object Orientation - OOPs
Page 30 of 36