Application Development 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: 

Abap Objects doubts.

Former Member
0 Kudos

Hi Experts.

Plz give me some details for the following questions.

1. what is abstract class.

2. give me some example program using inheritance.

3. Some sample programs using events.

4. Narrow casting & wide casting.. And describe the use of it.

5. How to declare internal table in ABAP OOPS.

6. What is the use of table type.?

7. What is instance.?

8. Explain friend class.

Thank in advance.

Points will be given for the answers.

regards,

J.Joe

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Go to Tcode 'abapdocu' and u will find all the example programs in abap objects.

The table type is used to declare internal table type globally so that u can refer it directly while creating the interrnal table in the program or global class.

Generally we cannot access private data in the class, if we want to use it then we have declare it has friend class,so generally we use it to access private data and methods in the class.

for further reference go to the website

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

reward points if it helps.

5 REPLIES 5

Former Member
0 Kudos

Hi,

Go to Tcode 'abapdocu' and u will find all the example programs in abap objects.

The table type is used to declare internal table type globally so that u can refer it directly while creating the interrnal table in the program or global class.

Generally we cannot access private data in the class, if we want to use it then we have declare it has friend class,so generally we use it to access private data and methods in the class.

for further reference go to the website

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

reward points if it helps.

asik_shameem
Active Contributor
0 Kudos

Hi

Former Member
0 Kudos

Hi Joe,

Please check the below links for your questions.

Thanks for the promised points.

1) Abstraction: An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. These abstract methods can be defined in the derived class. Example :public abstract class Parent{ public abstract void add();}public class child : Parent{ public override void add() { }}And not like this:public abstract class Parent{ public abstract void add() { }}public class child : Parent{ public override void add() { }}

http://www.geekinterview.com/question_details/13381

2,3) Goto ABAPDOCU transaction for sample programs.

4)

5)http://abapprogramming.blogspot.com/search/label/OOPS%20ABAP%201

6)http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb366d358411d1829f0000e829fbfe/content.htm

7)http://abapprogramming.blogspot.com/2007/10/oops-abap-5.html

😎 http://www.allinterview.com/showanswers/13829.html

Regards,

Esha

Former Member
0 Kudos

HI

<b>1. what is abstract class.</b>

Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract) subclasses, for example to define a uniform interface.

Classes with at least one abstract method are themselves abstract.

Static methods and constructors cannot be abstract.

You can specify the class of the instance to be created explicitly: CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>.

Abstarct classes themselves can’t be instantiated ( althrough their subclasses can)

Reference to abstract classes can refer to instance of subclass

Abstract (instance) methods are difined in the class , but not implemented

They must be redefined in subclasses

CLASS LC1 DEFINAITION ABSTARCT

PUBLIC SECTION

METHODS ESTIMATE ABSTARCT IMPORTING…

ENDCLASS.

<b>2. give me some example program using inheritance.</b>

The following simple example shows the principle of inheritance within ABAP Objects. It is based on the Simple Introduction to Classes. A new class counter_ten inherits from the existing class counter.

REPORT demo_inheritance.

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS: set IMPORTING value(set_value) TYPE i,

increment,

get EXPORTING value(get_value) TYPE i.

PROTECTED SECTION .

DATA count TYPE i.

ENDCLASS.

CLASS counter IMPLEMENTATION.

METHOD set.

count = set_value.

ENDMETHOD.

METHOD increment.

ADD 1 TO count.

ENDMETHOD.

METHOD get.

get_value = count.

ENDMETHOD.

ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.

PUBLIC SECTION.

METHODS increment REDEFINITION .

DATA count_ten.

ENDCLASS.

CLASS counter_ten IMPLEMENTATION.

METHOD increment.

DATA modulo TYPE I.

CALL METHOD super->increment .

write / count.

modulo = count mod 10.

IF modulo = 0.

count_ten = count_ten + 1.

write count_ten.

ENDIF.

ENDMETHOD.

ENDCLASS.

DATA: count TYPE REF TO counter,

number TYPE i VALUE 5.

START-OF-SELECTION.

CREATE OBJECT count TYPE counter_ten .

CALL METHOD count->set EXPORTING set_value = number.

DO 20 TIMES.

CALL METHOD count->increment.

ENDDO.

The class COUNTER_TEN is derived from COUNTER. It redefines the method INCREMENT. To do this, you must change the visibility of the COUNT attribute from PRIVATE to PROTECTED. The redefined method calls the obscured method of the superclass using the pseudoreference SUPER->. The redefined method is a specialization of the inherited method.

The example instantiates the subclass. The reference variable pointing to it has the type of the superclass. When the INCREMENT method is called using the superclass reference, the system executes the redefined method from the subclass.

<b>3. Some sample programs using events.</b>

Events in ABAP Objects - Example

The following example shows how to declare, call, and handle events in ABAP Objects.

Overview

This object works with the interactive list displayed below. Each user interaction triggers an event in ABAP Objects. The list and its data is created in the class C_LIST. There is a class STATUS for processing user actions. It triggers an event BUTTON_CLICKED in the AT USER-COMMAND event. The event is handled in the class C_LIST. It contains an object of the class C_SHIP or C_TRUCK for each line of the list. Both of these classes implement the interface I_VEHICLE. Whenever the speed of one of these objects changes, the event SPEED_CHANGE is triggered. The class C_LIST reacts to this and updates the list.

Constraints

The ABAP statements used for list processing are not yet fully available in ABAP Objects. However, to produce a simple test output, you can use the following statements:

WRITE [AT] /<offset>(<length>) <f>

ULINE

SKIP

NEW-LINE

Note: The behavior of formatting and interactive list functions in their current state are not guaranteed. Incompatible changes could occur in a future release.

Declarations

This example is implemented using local interfaces and classes. Below are the declarations of the interfaces and classes:

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

  • Interface and Class declarations

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

INTERFACE I_VEHICLE.

DATA MAX_SPEED TYPE I.

EVENTS SPEED_CHANGE EXPORTING VALUE(NEW_SPEED) TYPE I.

METHODS: DRIVE,

STOP.

ENDINTERFACE.

*----


CLASS C_SHIP DEFINITION.

PUBLIC SECTION.

METHODS CONSTRUCTOR.

INTERFACES I_VEHICLE.

PRIVATE SECTION.

ALIASES MAX FOR I_VEHICLE~MAX_SPEED.

DATA SHIP_SPEED TYPE I.

ENDCLASS.

*----


CLASS C_TRUCK DEFINITION.

PUBLIC SECTION.

METHODS CONSTRUCTOR.

INTERFACES I_VEHICLE.

PRIVATE SECTION.

ALIASES MAX FOR I_VEHICLE~MAX_SPEED.

DATA TRUCK_SPEED TYPE I.

ENDCLASS.

*----


CLASS STATUS DEFINITION.

PUBLIC SECTION.

CLASS-EVENTS BUTTON_CLICKED EXPORTING VALUE(FCODE) LIKE SY-UCOMM.

CLASS-METHODS: CLASS_CONSTRUCTOR,

USER_ACTION.

ENDCLASS.

*----


CLASS C_LIST DEFINITION.

PUBLIC SECTION.

METHODS: FCODE_HANDLER FOR EVENT BUTTON_CLICKED OF STATUS

IMPORTING FCODE,

LIST_CHANGE FOR EVENT SPEED_CHANGE OF I_VEHICLE

IMPORTING NEW_SPEED,

LIST_OUTPUT.

PRIVATE SECTION.

DATA: ID TYPE I,

REF_SHIP TYPE REF TO C_SHIP,

REF_TRUCK TYPE REF TO C_TRUCK,

BEGIN OF LINE,

ID TYPE I,

FLAG,

IREF TYPE REF TO I_VEHICLE,

SPEED TYPE I,

END OF LINE,

LIST LIKE SORTED TABLE OF LINE WITH UNIQUE KEY ID.

ENDCLASS.

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

The following events are declared in the example:

The instance event SPEED_CHANGE in the interface I_VEHICLE

The static event BUTTON_CLICKED in the class STATUS.

The class C_LIST contains event handler methods for both events.

Note that the class STATUS does not have any attributes, and therefore only works with static methods and events.

Implementations

Below are the implementations of the methods of the above classes:

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

  • Implementations

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

CLASS C_SHIP IMPLEMENTATION.

METHOD CONSTRUCTOR.

MAX = 30.

ENDMETHOD.

METHOD I_VEHICLE~DRIVE.

CHECK SHIP_SPEED < MAX.

SHIP_SPEED = SHIP_SPEED + 10.

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = SHIP_SPEED.

ENDMETHOD.

METHOD I_VEHICLE~STOP.

CHECK SHIP_SPEED > 0.

SHIP_SPEED = 0.

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = SHIP_SPEED.

ENDMETHOD.

ENDCLASS.

*----


CLASS C_TRUCK IMPLEMENTATION.

METHOD CONSTRUCTOR.

MAX = 150.

ENDMETHOD.

METHOD I_VEHICLE~DRIVE.

CHECK TRUCK_SPEED < MAX.

TRUCK_SPEED = TRUCK_SPEED + 50.

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = TRUCK_SPEED.

ENDMETHOD.

METHOD I_VEHICLE~STOP.

CHECK TRUCK_SPEED > 0.

TRUCK_SPEED = 0.

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = TRUCK_SPEED.

ENDMETHOD.

ENDCLASS.

*----


CLASS STATUS IMPLEMENTATION.

METHOD CLASS_CONSTRUCTOR.

SET PF-STATUS 'VEHICLE'.

WRITE 'Click a button!'.

ENDMETHOD.

METHOD USER_ACTION.

RAISE EVENT BUTTON_CLICKED EXPORTING FCODE = SY-UCOMM.

ENDMETHOD.

ENDCLASS.

*----


CLASS C_LIST IMPLEMENTATION.

METHOD FCODE_HANDLER .

CLEAR LINE.

CASE FCODE.

WHEN 'CREA_SHIP'.

ID = ID + 1.

CREATE OBJECT REF_SHIP.

LINE-ID = ID.

LINE-FLAG = 'C'.

LINE-IREF = REF_SHIP.

APPEND LINE TO LIST.

WHEN 'CREA_TRUCK'.

ID = ID + 1.

CREATE OBJECT REF_TRUCK.

LINE-ID = ID.

LINE-FLAG = 'T'.

LINE-IREF = REF_TRUCK.

APPEND LINE TO LIST.

WHEN 'DRIVE'.

CHECK SY-LILLI > 0.

READ TABLE LIST INDEX SY-LILLI INTO LINE.

CALL METHOD LINE-IREF->DRIVE.

WHEN 'STOP'.

LOOP AT LIST INTO LINE.

CALL METHOD LINE-IREF->STOP.

ENDLOOP.

WHEN 'CANCEL'.

LEAVE PROGRAM.

ENDCASE.

CALL METHOD LIST_OUTPUT.

ENDMETHOD.

METHOD LIST_CHANGE .

LINE-SPEED = NEW_SPEED.

MODIFY TABLE LIST FROM LINE.

ENDMETHOD.

METHOD LIST_OUTPUT.

SY-LSIND = 0.

SET TITLEBAR 'TIT'.

LOOP AT LIST INTO LINE.

IF LINE-FLAG = 'C'.

WRITE / ICON_WS_SHIP AS ICON.

ELSEIF LINE-FLAG = 'T'.

WRITE / ICON_WS_TRUCK AS ICON.

ENDIF.

WRITE: 'Speed = ', LINE-SPEED.

ENDLOOP.

ENDMETHOD.

ENDCLASS.

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

The static method USER_ACTION of the class STATUS triggers the static event BUTTON_CLICKED. The instance methods I_VEHICLEDRIVE and I_VEHICLESTOP trigger the instance event I_VEHICLE~SPEED_CHANGE in the classes C_SHIP and C_TRUCK.

Using the Classes in a Program

The following program uses the above classes:

REPORT OO_EVENTS_DEMO NO STANDARD PAGE HEADING.

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

  • Global data of program

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

DATA LIST TYPE REF TO C_LIST.

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

  • Program events

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

START-OF-SELECTION.

CREATE OBJECT LIST.

SET HANDLER: LIST->FCODE_HANDLER,

LIST->LIST_CHANGE FOR ALL INSTANCES.

*----


AT USER-COMMAND.

CALL METHOD STATUS=>USER_ACTION.

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

The program creates an object of the class C_LIST and registers the event handler method FCODE_HANDLER of the object for the class event BUTTON_CLICKED, and the event handler method LIST_CHANGE for the event SPEED_CHANGE of all instances that implement the interface I_VEHICLE.

<b>4. Narrow casting & wide casting.. And describe the use of it.</b>

<b>Narrowing Cast</b>

The assignment of a subclass instance to a reference variable of the type "reference to superclass" is described as a narrowing cast, because you are switching from a more detailed view to a one with less detail.

Description up-cast is also used.

<b>use</b>

A user who is not interested in the finer points of cars, trucks, and busses (but only, for example, in the fuel consumption and tank gauge) does not need to know about them. This user only wants and needs to work with (references to) the lcl_vehicle class. However, in order to allow the user to work with cars, busses, or trucks, you generally need a narrowing cast.

<b>Widening cast</b>

The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be checked statically, only at runtime. The Cast Operator ?= (or the equivalent MOVE ... ?TO … ) must be used to make this visible.

It changes from a less detailed view to one with more detail.

<b>use</b>

The client, the car rental company wants to execute a function for specific vehicles form the list (vehicle_list). For example, the client wants to ascertain the truck with the largest cargo capacity. However, not all vehicles are in the trucks list, it also includes references to cars and busses.

<b>5. How to declare internal table in ABAP OOPS.</b>

BEGIN OF LINE,

ID TYPE I,

FLAG,

IREF TYPE REF TO I_VEHICLE,

SPEED TYPE I,

END OF LINE,

<b>6. What is the use of table type.?</b>

Please check the SAP help , as all these things are provided in detail in it .

In case you find it difficult to find it or search for it here is the link

http://help.sap.com/saphelp_nw04/helpdata/en/90/8d7304b1af11d194f600a0c929b3c3/frameset.htm

<b>7. What is instance.?</b>

Instance methods are called using CALL METHOD <reference>-><instance_method>.

Static methods (also referred to as class methods) are called using CALL METHOD <classname>=><class_method>.

If you are calling a static method from within the class, you can omit the class name.

You access instance attributes using <instance>-><instance_attribute>

<b>8. Explain friend class.</b>

In rare cases, classes have to work together so closely that they need access to their protected and private components. To avoid making these components available to all users, there is the concept of friendship between classes.

To do this you use the FRIENDS additions to the CLASS statement, in which all classes and interfaces that are to be provided friendship are listed

In principle, providing friendship is one-sided: A class providing friendship is not automatically a friend of its friends. If a class providing friendship wants to access the non-public components of a friend, this friend has to explicitly provide friendship to it.

Classes that inherit from friends and interfaces that contain a friend as a component interface also become friends. However, providing friendship, unlike the attribute of being a friend, is not inherited. A friend of a superclass is therefore not automatically a friend of its subclasses.

<b>Reward if usefull</b>

Former Member
0 Kudos

Thank you all.....