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

difference in LIKE / TYPE / TYPE REF TO

Former Member
14,154

Hi All,

Kindly let me know what is the purpose of the below words in OOABAP.

LIKE

TYPE

TYPE REF TO

Akshitha.

5 REPLIES 5
Read only

Former Member
5,093

Hi,

ABAP distinguishes between types and objects. Types are descriptions that do not occupy memory. Objects are instances of types, and do occupy their own memory space. A type describes the technical attributes of all of the objects with that type.

You can use the addition

TYPE <type>

to refer to any data type <type> that is already known at this point in the program.

DATA <f> TYPE <type>.

The data object <f> has a data type corresponding to the type <type>.

DATA <f> LIKE <obj>.

The data object <f> inherits all of the technical attributes of the data object <obj>.

Take an example :

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab like ty_tab.

See here we declared the structure of ty_tab, which do not occupy memory. So if we run this, we will get compile time error like this : Field TY_TAB is unknown. It is neither in one of the specified tables nor defined by a DATA statement.

So in this case u need to correct the error with "TYPE" statement...like this.

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab type ty_tab.

Refer these

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

Regards,

Satish

Read only

Former Member
0 Likes
5,093
Read only

Former Member
5,093

Hi

Type ref to ..... is used to create objects of class

data : object1 type ref to class1.

*******************

*This is type definition do not hold any data its just a structure..

types : begin of itab,

f1 type c,

f2 type c,

end of itab.

Now you can use TYPE not LIKE for this because this is just a structure.defining an internal table/ DATA OBJECT

data : it_itab TYPE STANDARD TABLE OF itab. "you are using type to structures

Now another table

data ITAB2 LIKE it_itab. "you can use LIKE to data Objects

you can 't do like this

data itab2 like itab. "this is wrong

*********

type is generally used for declaring variables, parameters for existing data types in abap

for ex: to declare a inter value and character variable of length 10 is as,

data: i1 type i,

c1(10) type c.

like generally refers to existing data objects in abap.

for ex:

data: matnr like mara-matnr,

vbeln like vbap-vbeln.

creating variables matnr, vbeln from existing fields of tables mara, vbap.

when user creates a user defiend structure for work areas, internal tables we generally use type keyword as

types: begin of itab,

.........

.........

........

end of itab.

data: itab1 type itab occurs 0 with header line

.............

*********

Go through the link.

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .

LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.

Check this thread.

https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=2270752

https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=512214

Regards

Vasu

Read only

Former Member
0 Likes
5,093

hi

TYPE Addition

You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols.

Referring to Known Data Types

You can use the addition

TYPE <type>

to refer to any data type <type> that is already known at this point in the program. It can be used in any of the statements listed below. The expression <obj> is either the name of the data object or the expression

Definition of local program types using

TYPES <t> TYPE <type>.

Declaration of data objects using

DATA <f> TYPE <type>.

LIKE Addition

You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition

LIKE <obj>

can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

Definition of local types in a program using

TYPES <t> LIKE <obj>.

The new data type <t> inherits all of the technical attributes of the data object <obj>.

Declaration of data objects using

DATA <f> LIKE <obj>.

Objects

Objects are instances of classes. Each object has a unique identity and its own attributes. All transient objects reside in the context of an internal session (memory area of an ABAP program). Persistent objects in the database are not yet available. A class can have any number of objects (instances).

Object References

To access an object from an ABAP program, you use object references. Object references are pointers to objects. In ABAP, they are always contained in reference variables.

Reference variables

Reference variables contain references. A reference variable is either initial or contains a reference to an existing object. The identity of an object depends on its reference. A reference variable that points to an object knows the identity of that object. Users cannot access the identity of the object directly.

Reference variables in ABAP are treated like other elementary data objects. This means that a reference variable can occur as a component of a structure or internal table as well as on its own.

Data Types for References

ABAP contains a predefined data type for references, comparable to the data types for structures or internal tables. The full data type is not defined until the declaration in the ABAP program. The data type of a reference variable determines how the program handles its value (that is, the object reference). There are two principal types of references: Class references and interface references (see Interfaces).

You define class references using the

... TYPE REF TO <class>

eg:

data : cref type ref to class_name

addition in the TYPES or DATA statement, where <class> refers to a class. A reference variable with the type class reference is called a class reference variable, or class reference for short.

A class reference <cref> allows a user to create an instance (object) of the corresponding class, and to address a visible component <comp> within it using the form

cref->comp

Read only

matt
Active Contributor
5,093

TYPE - defines data with a specific type defined in the data dictionary

LIKE - defines data to have the same type as another data object already defined, or in the data dictionary

TYPE REF TO - defines that the data is a reference to a more complex object - that could be some data, a class or an interface. It's like a pointer to where the object is.

matt