Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Instantiation and Encapsulation

Former Member
0 Likes
3,764

Hi

Can any one explain me about instantiation and Encasulation with example, i am new to ABAP Objects.

Please dont send any links.

Arun Joseph

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,639

Encapsulation

Objects restrict the visibility of their resources (attributes and methods) to other users. Every object has an interface, which determines how other objects can interact with it. The implementation of the object is encapsulated, that is, invisible outside the object itself.

The three visibility areas - Public, Protected and Private, are the basis for one of the important features of object orientation - encapsulation. When you define a class, you should take great care in designing the public components, and try to declare as few public components as possible. The public components of global classes may not be changed once you have released the class.

For example, public attributes are visible externally, and form a part of the interface between an object and its users. If you want to encapsulate the state of an object fully, you cannot declare any public attributes.

As well as defining the visibility of an attribute, you can also protect it from changes using the READ-ONLY addition

Hi

Can any one explain me about instantiation and Encasulation with example, i am new to ABAP Objects.

Please dont send any links.

Arun Joseph

10 REPLIES 10
Read only

Former Member
0 Likes
2,639

Hi,

First let me give small introduction to OO ABAP programing.

Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class...?

A class is a set of objects that share a common

structure and a common behavior

A class will have attributes ( i.e data definition) and methods ( nothing but functions )

We can also call object as Instance of the class.

Lets see a simple demo class to calcualte SUM of 2 numbers.

define 2 variables a and b of type i. (Attributes)

SUM is the method name. this will have the logic to do the sum.

2 importing paramers and an exporting parameter res for the method.

Note: you can either create a class either globally or locally..

Goto SE24 to create Global class, this class will be avaialable for use in all the programs, this is like class library with all global classes.

Local class is the one which you create in SE38 i.e; your own program,

by this it is understood that it's scope is only upto that program

SO let us discuss the example with SE24(global class)

give a class name, description,

In the attributes tab, define a and b of type i, level as instance attribute and

visibility as public That means these attributes are available outside this class also. we will come to this later.

in the methods tab give name, level as instance, visibilty as pubilc again.

now double click on the method name, it will take to you to write the code.

(method implementation)

just wirte between the method ---endmethod,

res = num1 + num2.

come back to methods tab, click on parameters button, to define exporting, importing etc.. paramters for your method,

define 'res' as an exporting parameter, 2 variables num1 and num2 as importing parameters.Activate the class.

now you have defined a class.

whenever you want the logic to sum 2 variables, in all those cases you can make use of the SUM method in this class. This is what Reusability is.A very imp. feature of OO ABAP.

So now comes Instantiation.

you can't directly access a class.

You have to create a reference variable of the type of the class, then create an object with the reference variable. (memory allocation).

This step is called Instantiation or Instantiating the class

For this go to se38, in which ever program you want to instatantiate the above class.

do as below.

data: obj type ref to zsow_cl1.

start-of-selection.
create object obj.

Now object of the above class is ready for you.

Now all the contents i.e either attributes or methods etc of the class can be accessed with this object

now you have to call the method 'sum' of the above class using the above object in your program for the sum functionalty.

for that click on Pattern button select 'Abap Object Patterns' press enter

select 'Call method' radio button, give your object name ('OBJ')

class name ( ZSOW_CL1)

give the method name (SUM)

class and method names can be selected from the F4

press enter.

you will get the below line in your program

CALL METHOD OBJ->SUM
  EXPORTING
    NUM1   =
    NUM2   =
*  IMPORTING
*    RES    =
    .

now define 2 variables of type i to pass the inputs to the method and one more vairable to hold the output coming from the FM

on the whole code in your se38 is as follows.

DATA: OBJ TYPE REF TO ZSOW_CL1,
      X TYPE I,
      Y TYPE I,
      Z TYPE I.

START-OF-SELECTION.

  CREATE OBJECT OBJ.

  X = 10.
  Y = 10.

  CALL METHOD OBJ->SUM
    EXPORTING
      NUM1 = X
      NUM2 = Y
    IMPORTING
      RES  = Z.

  WRITE: Z.

so Z will give the output.

Next is Encapsulation

while defining the method i asked you to give the visibility as Public.

Visibility can be of 3 types:

Public

Protected

Private

public means as i said above the method or varaibles can be used outside the class also.as you did above outside the class you have created an object to the class and

accessed the method.

private means the attributes/methods that you define in the class can only be accessed in/by the method of the same class, they cannot be accessed anywhere else and outside.

change your 'a' parameter visibilty to private and try the below code in se38.

obj->b = 10.
obj->a = 10.

1st line doesn't give error, since 'b' is public attribte,

2nd line will give error since 'a' is now private.

you can use 'a' in the 'SUM' method if you want.

Protected means it can't be accessed outside of the class like with the help of object, but can be accessed in the child class if defined to the above class.

just post if you have any other doubts after trying this example.

    • Do reward points if it helps you.

Regards,

Sowjanya

Read only

0 Likes
2,639

Hi

I followed it up to certain levels,here is my code is

REPORT ZTESTCLASS .

data: test_class type ref to ztest0077.

start-of-selection.

create object test_class.

I am getting a error like this.

"You can not create an instance of the class ztest0077 outside the class..

Can u please clarify my problem.

Arun Joseph

Read only

Former Member
0 Likes
2,640

Encapsulation

Objects restrict the visibility of their resources (attributes and methods) to other users. Every object has an interface, which determines how other objects can interact with it. The implementation of the object is encapsulated, that is, invisible outside the object itself.

The three visibility areas - Public, Protected and Private, are the basis for one of the important features of object orientation - encapsulation. When you define a class, you should take great care in designing the public components, and try to declare as few public components as possible. The public components of global classes may not be changed once you have released the class.

For example, public attributes are visible externally, and form a part of the interface between an object and its users. If you want to encapsulate the state of an object fully, you cannot declare any public attributes.

As well as defining the visibility of an attribute, you can also protect it from changes using the READ-ONLY addition

Read only

Former Member
0 Likes
2,639

Hi,

I assume you created your class in SE24...

when you are in display mode of the class...

click on 'Properties' tab and check if the Instantiation parameter is 'Public'

or not...

it should be 'public' to be instanatiated...in the sense in order to be able to create an object of a class it should be given 'Public' as i expaloned you earlier...

by default it will be public... you might have change it to 'Private'....

just check it...

Regards,

Sowjanya

Edited by: sowjanya s on Apr 30, 2008 4:15 PM

Read only

0 Likes
2,639

Hi

As u said its in private so i have changed it to public now i got the output.

Can you please bit more explanative in public privare etc classes.

Thanks and Regards

Arun Joseph

Read only

0 Likes
2,639

The visibility areas define the external visibility of the class components, that is, the interface between the class and its users. Each component of a class must be assigned to one of the visibility sections.

Public Section

All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. The public components of the class form the interface between the class and its users.

Protected Section

All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it. Protected components form a special interface between a class and its subclasses. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.

Private Section

Components that you declare in the private section are only visible in the methods of the same class. The private components are not part of the external interface of the class.

CLASS <class> DEFINITION.

PUBLIC SECTION.

...

PROTECTED SECTION.

...

PRIVATE SECTION.

...

ENDCLASS.

Example:

CLASS C_COUNTER DEFINITION.

PUBLIC SECTION.

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I,

INCREMENT_COUNTER,

GET_COUNTER EXPORTING VALUE(GET_VALUE) TYPE I.

PRIVATE SECTION.

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER IMPLEMENTATION.

METHOD SET_COUNTER.

COUNT = SET_VALUE.

ENDMETHOD.

METHOD INCREMENT_COUNTER.

ADD 1 TO COUNT.

ENDMETHOD.

METHOD GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS.

The class C_COUNTER contains three public methods - SET_COUNTER, INCREMENT_COUNTER, and GET_COUNTER. Each of these works with the private integer field COUNT. Two of the methods have input and output parameters. These form the data interface of the class. The field COUNT is not outwardly visible.

I hope it helps.

Best Regards,

Vibha

Please mark all the helpful answers

Read only

0 Likes
2,639

Hi

What is the difference between declaring a class through se24 and declaring it in the program..

what is the advantage and disadvantage of declaring either ways??

Thanks and Regards

Arun Joseph

Read only

0 Likes
2,639

The Class Builder SE24 allows you to create and maintain global ABAP classes and interfaces. Both of these object types, like global data types, are defined in the ABAP Repository, thus composing a central class library. Together, they form a central class library and are visible throughout the system. You can display existing classes and interfaces in the class library using the Class Browser.

You can define local classes as well as global classes. They are defined locally in programs, function groups or as auxiliary classes of global classes of the class pools. Local classes are only visible within the defining module.

I hope it helps.

Best Regards,

Vibha

Please mark all the helpful answers

Read only

0 Likes
2,639

hi

its very simple....

PUBLIC :all the class hav actively use the public methods,variables.(e.g. public telephone: all of them can use it)

PRIVATE:only the CLASS member hav the permission to use.outside the class cant use it.(Private telephone: we never allow outside person to call i our own phone)

PROTECTED: its similarly like PRIVATE but the inherited class also access the protected class(we allow our child to access our personal phone: Here child is the inherited one)

In public visibility its better to use READ-ONLY to avoid unnecessary changes... (e.g. its like locking the outgoing to avoid misuse)

Read only

Former Member
2,639

Hi,

coming to Visibilty Section:

Whether it is an Attribute or Method, visibilty can be either of these:

Public, Private and Protected.

If it is a class in addition to the above ones there is one more i.e. 'Abstract'.

while creating a class you can assign it's visibilty as you saw in properties tab,

this tells about class's visibilty i.e. whether class can be accessed outside or not.

To be able to access the class outside, the only option is 'Public'.

If it set to 'Private', that means you are hiding it from getting accessed from outside.

here comes Encapsulation' technique...

If it is set to 'Protected', you cannot create object to this class, but you can create a subclass to this class, set that visibilty to 'Public', then create an object to that sub class.

with this object you can access the components of the super class (i.e earlier class with 'Protected' visibility')

Hope you got me...in that way you can access Protected class' also

If it is 'Abstract', that means there is no implementation for the methods of the class.

method is only defined, not written any code inside it, in that case class is called 'Abstract'.

This is one more feature of OO ABAP.

Abstract class acts like a template, any one can inherit the class i.e; can create subclass to it and implement the method in the subclass.

Agaian you can define this subclass as 'Public' and create object,

and if you want you can access super classes components her.

In the same way the visibilty plays the role for attribues and 'Methods'

In case of Attributes

if it is declared 'Public' you can access it outside the class with the help of object.

if it is declared 'Protected', you cannot access it outside the class with the help of object, but you can access it in the subclass.it will automatically come into the subclass attributes, so you can use it in any of the methods that you define in subclass.

if it is declared 'Private', you cannot access it outside the class with the help of object or in any other way, just it is available only in the class it is created.

In case of Methods

Same as Attributes.

Note:

One important property of the class is 'Final',

If it is checked that means class cannot be further inherited, that means you cannot create subclass to this class.

So uncheck it if you want to create a subclass to the class.

And while creating subclass, in the properties tab,

ther is a tab called 'Superclass', click that and mention the super class name her,

i.e which class you want to make sper to this class.

when you select a class in the sperclass tab, activate it and see, all the attributes and methods of the super class will automatically come and sit here,

in addition to that you can define your own attributes and methods now in this class.

This is Concept called 'Inheritence'.

So by now in all you came to know all the main features of OO ABAP.

which difeerentiate prog from normal approach.

To summarize they are.

1. Reusability

2. Encapsulation

3. Abstract

4. Inheritance

Try these and post if you still have any other doubts.

Difference between local and global class is:

if you defiine in Se38 that class wil be available only up to that program,

whereas if you declare it in SE24 it will be available globally, and there comes agaian the point of 'reusability'.

Differecne between normal FunctionModules and class-methods are:

If you create an FM via se37 it will be available to all the users, there is no way to hide it from accessing by others, but global classes( or methods), even if you define globally in SE24, there are many options to hide them from being accessed by others like an example making it 'Private'.

here comes 'Encapsulation' which is not possible in normal ABAP.

Hope you are more clear now.

Regards,

Sowjanya

Edited by: sowjanya s on Apr 30, 2008 5:45 PM