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

Visibilty, Static attributes and methods, abstract classes

lakshminarasimhan_n4
Active Contributor
0 Likes
2,328

Hi Gurus,

we have 3 visibility in ABAP objects.

"Public" can be accessed by all of the methods of the class,by its sub class and can be accessed outside of the class via the instances of that class.

"private" can be touched only by the methods within the same class.

"protected" can be accessed by the methods of the class and by its sub class.

What i want to know is, when a class inherits another class then the protected members of the super class comes to sub class and they take the "private" visibility. For example, Suppose class A is inherited by class B. Then all the protected members of class A comes to class B under the private visibility.Now if another class C tries to inherit B, then it cannot receive the protected members of the class A as they have taken the private visibility in Class B.

Please let me know whether my understanding is correct.

Static methods or Static attributes comes under public or protected visibility. They cannot be redefined by the inheriting classes. Is my understanding correct?

Methods cannot be both final and abstract. But classes can be both abstract and final!!!

How could that be possible??

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,541

When you inherit a sub-class (C from B), the protected members of the grandparent (A) are still visible in the grand child (C). What happens when you first inherit is that the first sub-class (B) is set to final automatically (this is what happened to me when I tested). All you need to do is to uncheck the "final" checkbox in B and then when you inherit it into C, it should work just fine.

4 REPLIES 4
Read only

Former Member
0 Likes
1,542

When you inherit a sub-class (C from B), the protected members of the grandparent (A) are still visible in the grand child (C). What happens when you first inherit is that the first sub-class (B) is set to final automatically (this is what happened to me when I tested). All you need to do is to uncheck the "final" checkbox in B and then when you inherit it into C, it should work just fine.

Read only

Ramneek
Product and Topic Expert
Product and Topic Expert
0 Likes
1,541

Hello,

...if another class C tries to inherit B, then it cannot receive the protected members of the class A as they have taken the private visibility in Class B. Please let me know whether my understanding is correct.

No, the Class C will have access to the protected members defined in Class A. Protected members would remain protected for the entire heirarchy.

Static methods or Static attributes comes under public or protected visibility. They cannot be redefined by the inheriting classes. Is my understanding correct?

Static components can be private as well. A class which defines public or protected static attributes shares it with all its subclasses. Static methods cannot be redefined. Static methods cannot be abstract because they cannot be redefined.

Methods cannot be both final and abstract. But classes can be both abstract and final!!! How could that be possible??

Methods can be prevented from being redefined by using the FINAL addition with the METHODS statement. Since methods marked as FINAL cannot be redefined making them ABSTRACT as well would imply that no subclass would be able to implement it. Hence it is not allowed.

Classes can be both abstract and final. Such classes will only contain static components.

Hope this helps.

Thank you,

Ramneek

Read only

Clemenss
Active Contributor
0 Likes
1,541

Hi all of you,

PUBLIC means public - everybody everywhere has access, in a way comparable to global data. If the class is defined globally with class builder, it can be used system-wide in every report/function/class/module.

An inherited class may use and also redefine all inherited (public & protected) objects, but not on interface level. That means you can not change the attributes because the attribute id the interface by itself. You can redefine the coding of a method, but not the parameters in the methods interface (aka signature).

When you try, you will notice, that the class builder will set the FINAL property by default - I remove it by default. But this is kind of dangerous because everybody may inherit their own class and if I change my class the compatibility with the inheried classes and their use is not guaranteed.

Regards,

Clemens

Read only

Former Member
0 Likes
1,541

An easier way to answer your questions is to try it. Find a standard SAP class that is not final and start inheriting. You might want to keep your objects as $TMP and delete them after your questions have been answered.