Why to go for object oriented programming from traditional coding?
- Faster development and maintenance - Simplified modeling of the process - Reusability of code OO extension to ABAP is available from release 4! "ere the ABAP programs are organi#ed as collections of ob$ects Ob$ects are the instances of classes %lass is a template for set of ob$ects &hich share common structure and common behavior %lasses can be created in se'4 &hich serves as a global class %lasses also can be created inside ABAP program &hich are local to the program A class has mainly three sections namely private (accessible only &ithin the class)* public (can be accessed outside the class + global data) and protected (can only be accessed by itself and its subclass) ,ach section can have attributes (data)* methods (behavior-functionality)* events* exceptions ain OOP! concepts are" . Abstraction " Simplifying the complex reality by modeling in to classes appropriate to the problem %lasses are to be created only &ith essential characteristics &hich distinguishes it from others ' #ncaps$lation " /t0s a protective &rapper &hich prevents the code and data from arbitrary access by other code outside of &rapper Ob$ects are made up of attributes and methods 1he publicly defined attributes and methods acts as an interface* &hich ob$ects can be accessed outside 1he private section is used to 2eep data and methods to 2eep it safe from random access 1he actual implementation is encapsulated means invisible from outside and cannot be accessed directly3 the public interface determines ho& other ob$ects can interact &ith it 4 %nheritance" /t is the method by &hich a class can get the attributes and behavior from a predefined class 1he inherited subclass can also have its o&n attributes and behavior along &ith its parent super class0s attributes and behaviors 4 Polymorphism Ability of ob$ects belonging to different data types to respond to method calls of methods &ith same name* each one according to an appropriate type specific behavior %nstance Attrib$te &! !tatic Attrib$te" 1here exist only one instance attribute for each instance of a class and there exist one static attribute-class data per each class 5ormally static attributes contains information &hich is applicable to all instances such as types* constants* administrative information* central data etc %nstance ethod' !tatic (Class ethod and )$nctional ethod" /nstance method can access both static and instance components &hereas static method can only access static components Also for static method access there is no need of instance &hich means it can directly assess through the class 6ethod &hich has a returning parameter is termed as functional method 1his can0t have exporting and changing parameter /t follo&s pass by value Constr$ctor and Class Constr$ctor" %onstructor is a special method &hose name is al&ays %O5S1R7%1OR /t is used to instantiate the ob$ect /t is al&ays defined in public section and it can have only importing parameters and exceptions 8henever an ob$ect is instantiated using creates ob$ect statement* system creates an ob$ect and then it calls the %O5S1R7%1OR 8hen exceptions are raised instances are not created so no memory space is allocated 8hereas class constructor is a static constructor &hich automatically gets accessed &hen the system accesses the class for the first time Static constructor can0t have any parameters and exceptions /t can0t be called explicitly %nstance #*ents and Class #*ents" Both instance events and static events can be triggered in instance methods but static methods can only trigger static-class events An event can be public-private-protected 1he event handler can have the same visibility or restricted visibility than the event to &hich it refers 8henever an event is triggered then the corresponding handler methods that are registered to it are called in se9uence Abstract Class' )inal class and )riend Class" Abstract class can0t be instantiated Abstract class should contain at least one abstract method (method &ithout implementation) 1he subclass of abstract class can be instantiated if they are not abstract Final class can0t be inherited further All methods of final class are inherently final Final method can0t be redefined further /f a method in a class is final then that class can be inherited but the method can0t be redefined 1herefore to restrict the functionalities from changes the method can be made as final 1o access the public and protected attributes-methods of one class &ithout inheritance * FR/,5:S addition can be used for the class definition of a class &ith the name of the other class &hose components needs to be accessed An interface can also be made friend to a class 1he inherited subclass of a class &hich is used as friend for another class* are by default becomes friends of the class +arro,ing Cast &! Widening Cast" 5arro&ing cast- 7p cast refers to assigning the instance of a subclass bac2 to the instance of a super class meaning &e are going from a more specific vie& of an ob$ect to less specific vie& 1he opposite is &idening cast or do&n cast #-ceptions .andling" ,xception refers to a situation that arises &hile a program is being executed* &hen there is no point in continuing to run the program in normal &ay %lass based exceptions are raised by RA/S, ,;%,P1/O5 statement or by runtime environment 8henever an exception is raised an exception ob$ect is created &hose attribute hold the error situation 1ry ,nd try bloc2 is used to declare a protective section in the code &here one can catch one or more exceptions using %A1%" statement 8hen more than one exception is triggered they must be caught in specific generic class se9uence order else system generates error message saying <:eclare exceptions in ascending order0 All exceptions are inherited from class %;=ROO1 &hich has certain predefined methods such as >,1=1,;1* >,1=SO7R%,=POS/1/O5 &hich are inherited by all exception classes 8hen an exception is raised the normal program flo& is interrupted by system and it tries to navigate to suitable handle &hich if not found a runtime error occurs 1ry- ,nd try bloc2 can be nested 1here is a %?,A5 7P statement &ithin this bloc2 &hich is used to remove the references before leaving the method %;=ROO1 has three immediate subclass to one of &hich the exceptions normally refer 1hey are . %;=S1A1/%=%",%@ 1his type of exception has to be explicitly defined in the method signature /f not declared* it has to be caught else it &ill result in to run time error %ompiler can identify this type of exception so if declaration or catching is missed then it sho&s a &arning ' %;=:A5A6/%=%",%@ 1his type of exception has to be explicitly defined in the method signature /f not declared* it &on0t get chec2ed in runtime 4 %;=5O%",%@ 1hese must not be declared /f declared gives syntax error 1/ Constr$ctor %onstructor is a special instance method and al&ays named %O5S1R7%1OR /t is automatically called at runtime &ith %R,A1, OBB,%1 statement /t must be defined in public area /t can have only importing parameters and exceptions On the other hand static constructor or class constructor is al&ays named %?ASS=%O5S1R7%1OR /t is called automatically &hen the class is accessed for the first time /t can0t have parameters or exceptions /t can0t be called explicitly