Sie sind auf Seite 1von 11

Visibility of Methods in Global Classes

By Anil Bharadwaj, OpenText Technologies


This is a very basic example in the OO concepts in ABAP. In this example we deal with the visibility of classes and how we can use them in our Custom Programs and how a constructor and Friend Function work along with inheritance using Global Classes. Create Global Class in Transaction code SE24.

Select the type as Class

Make the class as Public

Here in the class I had created 4 methods with visibility as public, private, protected and a static method. We have 2 Levels options and 3 types Visibilities while creating Methods Levels: 1. Instance method: For these methods we have to create object i.e. Instance while using the class in the program. We can have any number of Instances of a class in a program. 2. Static method: For these we no need to create an object i.e. Instance, directly we can use the method with the class name. this does not support polymorphism and has many functionalities which are not needed at this stage.

Visibilities: Public: These methods are accessible trough out the program with instances and for subclasses and friends. Private: These Methods can be used only within the class where it is declared like the other methods of the class and these methods cannot be used with object reference from your Z Programs. Below is the screen of the code of the public method which is using the private and protected methods of its own class.

Now I had created another class which inherits the above class. You can see the super class name.

Click here to continue... Automatically all the visible methods of the super can be seen in the methods section of this class. The visible methods will be Public, Protected and Static Methods. If you want to add more functionality you can redefine this by selecting the method and pressing the rounded button below. You cannot redefine the static method of the super class.

Here I had created a method zinh_public where I code that in the below way. Both the methods Zpublic and Zprotected which area Public and protected methods of super class can be called as it own methods. So we called it as me->***** We can also call Static methods from the subclass methods as its own methods but we cannot access private methods .private methods of a class can be accessible to only friends of the class.

Now I had created a simple report which uses the above created global classes and its methods. In the below report I had created an instance of the super class and tried to call the Private and protected methods. System gives the errors as below.

Click here to continue... The output is displayed as below.

Now we shall see how to access a private method of a class. This can be achieved through friend function. Now create another class ZSAPTECH_FR.in the previous class zsaptech I declared new class zsaptech as friends.

Now in the zsaptech_fr I created a method as instance and public visibility.

In the implementation of the method I write the code to access the private method of the class zsaptech.

The code of the zprivate method in the zsaptech main class is as below.

Now in the report program I will create an instance of the new class and call the method.

You can see the output. The constructor of the main class also triggered as we had a constructor for the main class.