OO ABAP 23 March 2012Shri

34
Thanks, Object Oriented Programming with ABAP Compare car parts with assembled car ; which is better to understand ? By Shrinivas Kendre

Transcript of OO ABAP 23 March 2012Shri

Page 1: OO ABAP 23 March 2012Shri

Thanks,

Object Oriented Programming with ABAP

Compare car parts with assembled car ;

which is better to understand ?

ByShrinivas

Kendre

Page 2: OO ABAP 23 March 2012Shri

Thanks,

Abstraction and Encapsulation

Page 3: OO ABAP 23 March 2012Shri

Thanks,

• Classes• Objects/ Instances• Constructors• Inheritance• Polymorphism• Interface• Abstract Class• Final Class• Events

OOP’s Concepts !

Page 4: OO ABAP 23 March 2012Shri

It’s a template or blue-print of a Object which contains attributes, methods and events.

Class

Syntax

Page 5: OO ABAP 23 March 2012Shri

Thanks, If car is a Class ,then SUV, Hatch back, Sedan, Convertible are objects • Instance of a class

• It’s a variable of a type class

• Objects can be created and addressed using reference variables.

Object

Syntax

Page 6: OO ABAP 23 March 2012Shri

Thanks,

Attributes

• Attributes describe the data that can be stored in the objects in a class.

• Types– Instance attributes– Class attributes

• Visibility– Private– Public

Page 7: OO ABAP 23 March 2012Shri

Thanks, Attributes and visibility

Page 8: OO ABAP 23 March 2012Shri

Thanks, Attributes and visibility

Page 9: OO ABAP 23 March 2012Shri

Thanks,

Attributes

• Attributes describe the data that can be stored in the objects in a class.

• Types– Instance attributes– Class attributes

• Visibility– Private– Public

Page 10: OO ABAP 23 March 2012Shri

Thanks,

• Methods are internal procedures in classes that determine the behavior of an object

• Methods have a parameter interface• Types

– Instance method– Class method

• Visibility– Private– Public

Methods

Page 11: OO ABAP 23 March 2012Shri

Thanks,Methods -visibility

Calling Methods :Syntax

Page 12: OO ABAP 23 March 2012Shri

Thanks,Methods –Instance/class

Page 13: OO ABAP 23 March 2012Shri

Thanks,Calling a Method

Calling Methods :Syntax

Page 14: OO ABAP 23 March 2012Shri

Thanks,Calling a Method

Page 15: OO ABAP 23 March 2012Shri

Thanks,

Constructor

Page 16: OO ABAP 23 March 2012Shri

Thanks,Static Constructor

• Named as CLASS_CONSTRUCTOR• Called automatically before the class is first accessed.

– Before any of the following actions• CREATE OBJECT• Addressing static attribute cl_sample=>name.• Addressing static method CALL METHOD cl_sample=>set_name• Before registering static event handler using SET HANDLER

– <class name>=><handler method> FOR obj

Page 17: OO ABAP 23 March 2012Shri

Thanks,

Inheritance

• Inheritance is a relationship in which one class (the subclass) inherits all the main characteristics of another class (the superclass).

• The subclass can also add new components (attributes, methods, and so on) and replace inherited methods with its own implementations.

• The inheritance relationship is often described as an “is-a” relationship: a passenger plan is an airplane.

Page 18: OO ABAP 23 March 2012Shri

Thanks,Inheritance –

Visibility

• The constructor of the super class must be called within the constructor of the subclass.

• For static constructors, unlike instance constructors, the static constructor in the super class is called automatically.

Page 19: OO ABAP 23 March 2012Shri

Thanks,

Polymorphism

• Ability to take multiple forms• Same method name with different functionality• Achieved using the keyword REDEFINITION• Same method name is not allowed use in the sub classes.

Page 20: OO ABAP 23 March 2012Shri

Thanks,

Polymorphism –Tips

• The REDEFINITION statement for the inherited method must be in the same SECTION as the definition of the original method.

• (It can therefore not be in the PRIVATE SECTION, since a class’s private methods are not visible and therefore not redefinable in subclasses!)

• If you redefine a method, you do not need to enter its interface again in the subclass, but only the name of the method. The reason for this is that ABAP Objects does not support overlaying (see notes to previous slide).

Page 21: OO ABAP 23 March 2012Shri

Thanks,

Interface

• Basis for polymorphism• Achieve Multiple Inheritance• Independent structures that allow you to enhance the class• The class which implements this interface will provide a

implementation to this method

Page 22: OO ABAP 23 March 2012Shri

Thanks,

Abstract Class

• Cannot be instantiated• Used in incomplete blueprint • The subclass needs to redefine the undefined methods• Can have abstract or instance methods• Classes with at least one abstract method are themselves abstract• Static methods and constructors cannot be abstract (they cannot be

redefined).

Page 23: OO ABAP 23 March 2012Shri

Thanks,

Final Class

• A final class cannot have subclasses, and can protect itself in this way against (uncontrolled) specialization.

• A final method in a class cannot be redefined in a subclass, and can protect itself in this way against (uncontrolled) redefinition.

• Some characteristics:• A final class implicitly only contains final methods. You cannot enter

FINAL explicitly for these methods in this case.• Methods cannot be both final and abstract.• Classes, on the other hand, can usefully be both abstract and final:

only static components can be used there.

Page 24: OO ABAP 23 March 2012Shri

Thanks,

Events

Page 25: OO ABAP 23 March 2012Shri

Event Registration

Triggering an event

Handling event

Page 26: OO ABAP 23 March 2012Shri

Thanks,Global Class vs Local Class

Local

Glo

bal

Page 27: OO ABAP 23 March 2012Shri

Global Class – Class Builder

Page 28: OO ABAP 23 March 2012Shri

Class Edior– Step by Step Demo

SE24 - Class Builder

Page 29: OO ABAP 23 March 2012Shri

Class Edior– Most important thing in programmer’s life

• Copy /Convert Local Class to Global Class• Copy Super class to Sub class• Copy class methods/attributes among associated class• Demo :

SE24 -Demo -Refactoring

Page 30: OO ABAP 23 March 2012Shri

Show me The Code !!!!

Program Name Concept Source Code

ZKENDRE_OOABAP_ATTRIBUTE Class Attribute

ZKENDRE_OOABAP_METHOD Class Method

ZKENDRE_OOABAP_CONSTRUCTOR Class Constructor and Constructor

ZKENDRE_OOABAP_INHERITANCE Inheritance

ZKENDRE_OOABAP_POLYMORPHISM Polymorphism

ZKENDRE_OOABAP_INTERFACE Interface

Class Attribute Demo Program

Class Method Demo

Constructor DemoUML Diagram

Inheritance

Polymorphism

Interface

Page 31: OO ABAP 23 March 2012Shri

Show me The Code !!!!

Program Name Concept Source Code

ZKENDRE_OOABAP_FINAL Final Class

ZKENDRE_OOABAP_ABSTRACT Abstract Class

ZKENDRE_OOABAP_EVENT Event Concept

ZKENDRE_OOABAP_EVENT_EDITALVEditable ALV using OO ABAP events

ZKENDRE_OOABAP_GLOBAL_CLASS Global class

ZKENDRE_OOABAP_LOCAL_T_GLOB Convert local class to Global

Final

Abstract

Event

Advance Event - Editable ALV

Call Global class in Program

Convert local to global

Page 32: OO ABAP 23 March 2012Shri

Let’s have some hand’s on on system

Page 33: OO ABAP 23 March 2012Shri

There is lot to dig!!!!

• Binding : http://www.saptechnical.com/Tutorials/OOPS/Binding/Procedure.htm • Casting :• Garbage collector• Friend class…………………etc !!!

Page 34: OO ABAP 23 March 2012Shri

Thanks,

Oops …………. The End