Friend class
Hi to all,
I have created two classes 1) ztest and 2) ztest_cl.
I have declared 3 private variables of data type i say n1, n2, n3 .i gave Visibility as private for all three variables. And also in the ztest class i have declared ztest_cl as a friend class in friend attribute.
Now i want to use those data variables in one of the method in the ztest_cl.
I am writing code some thing like this n3 = n1 + 2 without declaring these parameters in ztest_cl class.
But its showing a error that data types (n1 and n2 and n3) unknown.
As it is friend class it should accept those variables but its not taking those variables.
Can any one suggest me please?
Thanks & Regards
Priya
HI
FRIEND CLASS is a concept useing which a child can access private components of the base class in it
FRIENDSHIP is extended by parent to the child
FRIENDSHIP is not transtive
VISIBILITY is allways 1st preference in the object oriented programing
the PRIVATE components are not inherited to the child class but thriugh that child class we can acess PRIVATE components of the BASE CLASS
*For this we need to create an object of the BASE CLASS with in the child class to access PRIVATE components *
FRIENDS key word along with the child class name is used in the parent clas definition section to consider a child as a friend
Similar Messages
-
Code to call friends class method
Hello,
Please can someone help me with friends class method call. in global classes
Thanks
Megh
Moderator message: please search for available information/documentation before asking.
Edited by: Thomas Zloch on Nov 2, 2010 5:29 PMWhat is your exact problem?
The class whose private and protected section you want to use, needs declaration of its friends. You do it in Friends tabstrip. All these classes are allowed then to access these "hiden" sections from outside of the class. Below example how it works with local classes. For global ones the principle stays the same
CLASS lcl_main DEFINITION DEFERRED.
CLASS lcl_other DEFINITION FRIENDS lcl_main. "only LCL_MAIN can use may hiden sections
PROTECTED SECTION.
METHODS prot_meth.
PRIVATE SECTION.
METHODS priv_meth.
ENDCLASS.
CLASS lcl_other IMPLEMENTATION.
METHOD prot_meth.
WRITE / 'This is protected method of class lcl_other'.
ENDMETHOD. "prot_meth
METHOD priv_meth.
WRITE / 'This is private method of class lcl_other'.
ENDMETHOD. "priv_meth
ENDCLASS.
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
METHODS call_friends_methods.
PRIVATE SECTION.
DATA mr_my_friend TYPE REF TO lcl_other.
ENDCLASS.
CLASS lcl_main IMPLEMENTATION.
METHOD call_friends_methods.
CREATE OBJECT mr_my_friend.
mr_my_friend->prot_meth( ).
mr_my_friend->priv_meth( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA gr_main TYPE REF TO lcl_main.
CREATE OBJECT gr_main.
gr_main->call_friends_methods( ).
Regards
Marcin -
Use of Friend class in Class builder
HI
Can someone tell me how to use a friend class In class Builder
SRidharHi,
Please check this link
http://help.sap.com/saphelp_nw04/helpdata/en/b5/693ec8185011d5969b00a0c94260a5/frameset.htm
Defining Relationships Between Object Types
http://help.sap.com/saphelp_nw04/helpdata/en/ca/c035b7a6c611d1b4790000e8a52bed/frameset.htm
Examples
http://help.sap.com/saphelp_nw04/helpdata/en/47/5f643a7c5dd813e10000000a114084/frameset.htm
Best regards,
raam -
In class builder what is the use of friend class and where can i find the standard classes and methods
Being someone's friend, means being allowed to use his protected attributes and method.
When you define a friend for a class, this class can addres these components as if they were public ones.
In real world: my friend (some class) can use my car (which is protected), which is not allowed to strangers (other classes).
Regards
Marcin -
Hi all,
we have diff. syntaxes to make friend class say..
CLASS A DEFINTION CREATE PRIVATE FRIEND CLASS B.
CLASS A DEFINTION FRIEND CLASS B.
Pleas reply..
<b>Have ur point.</b>
Regards,
pradeep phogatThe specified object types (classes or interfaces) obj_type_1 ... obj_type_n are declared as friends of the current class. In this way, these object types and all the object types derived from these through inheritance, inclusion, and implementation are allowed to access PROTECTED and PRIVATE components, in addition to PUBLIC components. They can also create instances from the class, irrespective of the specifications in the CREATE addition. All classes and interfaces from the same program as well as global classes and interfaces from the class library can be specified as object types.
Note
If you define global classes and class library interfaces as friends, you should note that the local classes of other ABAP programs will not be visible in them. A static access to the componenents of the class class is not possible in such friends.
Example
interface I1.
endinterface.
class C1 definition create private friends I1.
private section.
data A1(10) type C value 'Class 1'.
endclass.
class C2 definition.
public section.
interfaces I1.
methods M2.
endclass.
class C2 implementation.
method M2.
data OREF type ref to C1.
create object OREF.
write OREF->A1.
endmethod.
endclass.
data OREF type ref to C2.
start-of-selection.
create object OREF.
OREF->M2( ).
In this excample, classs C2 is a friend of the interface
I1 and therefore of the implementing class C1. It can
instance these and also access their private components.
Addition 7
... GLOBAL FRIENDS publ_obj_type_1 ... publ_obj_type_n
Effect
This addition must be used only for the definition of global classes. It cannot, therefore be specified locally with the ABAP Editor in an ABAP program, but is automatically generated during the definition of a global class using the tool Class Builder of the ABAP workbench. The specified global object types (classes or interfaces) publ_obj_type_1 ... publ_obj_type_n are declared as friends of the current class. In this way, these objects and all the object types derived from these through inheritance, inclusion, and implementation are allowed to access PROTECTED and PRIVATE components, in addition to the PUBLIC components. They can also create instances from the class, irrespective of the specifications in the addition CREATE
Rewards if useful.......................
Minal -
Accessing private attribute of a class from its Friend Class
Hi Experts ,
Please help me to understand how can i access private attribute of one class from its friend class.
I am coding in Method (DO_SAVE) of class /BOBF/CL_TRA_TRANSACTION_MGR.
I need to access private variable ( MO_BOPF) of class /BOBF/CL_TRA_SERVICE_MGR ( Friend of /BOBF/CL_TRA_TRANSACTION_MGR ).
Regards,
Reny RichardHi Reny,
You should be able to access by creating object of friend class.
Sample:
data lo_frnd TYPE REF TO /BOBF/CL_TRA_SERVICE_MGR.
data lo_compl type REF TO /BOBF/IF_TRA_TRANS_MGR_COMPL.
create OBJECT lo_frnd
exporting
iv_bo_key = '111'
IO_COMPL_TRANSACTION_MANAGER = lo_compl.
"access the private object of friend class
clear lo_frnd->MO_BOPF.
Note: need to provide iv_bo_key & IO_COMPL_TRANSACTION_MANAGER while creating object.
Hope this helps you.
Regards,
Rama -
How to access private attribute of a class from its Friend Class
Hi Experts ,
I am coding in Method (DO_SAVE) of class /BOBF/CL_TRA_TRANSACTION_MGR.
I need to access private variable ( MO_BOPF) of class /BOBF/CL_TRA_SERVICE_MGR ( Friend of /BOBF/CL_TRA_TRANSACTION_MGR ).
Please help me to understand how can i access private attribute of one class from its friend class.
Regards- AbhishekHi Reny,
You should be able to access by creating object of friend class.
Sample:
data lo_frnd TYPE REF TO /BOBF/CL_TRA_SERVICE_MGR.
data lo_compl type REF TO /BOBF/IF_TRA_TRANS_MGR_COMPL.
create OBJECT lo_frnd
exporting
iv_bo_key = '111'
IO_COMPL_TRANSACTION_MANAGER = lo_compl.
"access the private object of friend class
clear lo_frnd->MO_BOPF.
Note: need to provide iv_bo_key & IO_COMPL_TRANSACTION_MANAGER while creating object.
Hope this helps you.
Regards,
Rama -
Hi there,
I have a main_class that will be calling the methods of another little_class. This little_class need to use an object and a variable from my main_class. How could I do that. I know that in C++ what you need to do is just to tell that the main_class is a friend of little_class. But I don't how to do this in java.
I have also tryied to declare my variable var1 and my little_class as a global instance in my main_class as follow:
main_class
public:
Transform3D var1=null;
Little_Class little=null;
main_method()
var1=new Transform3D();
//etc
little=new Little_Class();
Little_Class
little_method()
Transform3D var2=new Transform3D();
var2=var1;
But the debuger get the error that my var1 wasn't declared. Some hints ? Thnx in advance....�:0).ThomasThere are many ways to do this, but in general it can be considered bad design.
To make a member accessible to another class, you can either:
a) make the member public (makes it accessible to entire world)
b) provide a public accessor method (i.e. a "getter")
c) make the member protected which only allows derived classes and classes in the same package access to the method
d) if one class is the inner class of an outer class, you can pass a reference to the outer class into the inner class so that it can access the member
See, the problem with your original post:
class outer {
SomeObject var1 = null;
class inner {
SomeObject var2 = null;
public inner() {
var2 = var1;
}Is that the inner class is not related to an instance of the outer class, but var1 is an instance variable (not a class variable). You would need to pass a reference down to the inner class:
class outer {
SomeObject var1 = null;
class inner {
SomeObject var2 = null;
outer outerRef = null;
public inner(outer _ref) {
outerRef = _ref;
var2 = outerRef.var1; -
Error while usind Private Method of a global class
HI All..
I created a global class (ZLINE_GLOBAL) which has TOT_DATA private method. I have to call this private method in my report, I know that using Friend class we can do this.
But it is not working and showing the same error " METHOD "TOT_DATA" is unknown or Private or Public..
code i tried is
CLASS c2 DEFINITION DEFERRED.
CLASS ZLINE_GLOBAL DEFINITION FRIENDS c2.
PUBLIC SECTION.
METHODS : m1.
PRIVATE SECTION.
METHODS: m2.
ENDCLASS.
CLASS ZLINE_GLOBAL IMPLEMENTATION .
METHOD m1.
WRITE : 'Public Method C1'.
ENDMETHOD. "M1
METHOD m2.
WRITE : 'Private Method C1'.
ENDMETHOD.
ENDCLASS.
CLASS c2 DEFINITION FRIENDS ZLINE_GLOBAL. "my friends are here, allow them access to my (C2's) private components
PUBLIC SECTION.
METHODS :m3.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m3.
DATA : obj TYPE REF TO ZLINE_GLOBAL.
CREATE OBJECT obj.
CALL METHOD obj->TOT_DATA. "here Iam calling Private method of global class
ENDMETHOD. "M3
ENDCLASS.
START-OF-SELECTION.
DATA obj_c2 TYPE REF TO c2.
CREATE OBJECT obj_c2.
obj_c2->m3( ).
can anybody help me on this..
MurthyHi Murthy,
Replace TOT_DATA with M2, you do not have any method by name "TOT_DATA" in your code.
CLASS c2 DEFINITION DEFERRED.
CLASS ZLINE_GLOBAL DEFINITION FRIENDS c2.
PUBLIC SECTION.
METHODS : m1.
PRIVATE SECTION.
METHODS: m2.
ENDCLASS.
CLASS ZLINE_GLOBAL IMPLEMENTATION .
METHOD m1.
WRITE : 'Public Method C1'.
ENDMETHOD. "M1
METHOD m2.
WRITE : 'Private Method C1'.
ENDMETHOD.
ENDCLASS.
CLASS c2 DEFINITION FRIENDS ZLINE_GLOBAL. "my friends are here, allow them access to my (C2's) private components
PUBLIC SECTION.
METHODS :m3.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m3.
DATA : obj TYPE REF TO ZLINE_GLOBAL.
CREATE OBJECT obj.
CALL METHOD obj->M2. "here Iam calling Private method of global class
ENDMETHOD. "M3
ENDCLASS.
START-OF-SELECTION.
DATA obj_c2 TYPE REF TO c2.
CREATE OBJECT obj_c2.
obj_c2->m3( ).
Regards,
Chen -
Methods of class contained in an other class
Hello,
i would like to know is there is another way than what i do with classes embeded in another:
ex i have a device classe that containe a class Com (rs232 ...)
what i do whan i want to setup a com of a device is get com objet of device then appliing change on COM with com methodes
is there a way to apply com methode directly to Device ? i thought about putting get instr method in Com class so unbundle is done directly
but i don't think this is a good thing ...
Is there something to do with friend class or community ?
see attached .jpg
Regards
Tinnitus
CLAD / Labview 2011, Win Xp
Mission d'une semaine- à plusieurs mois laissez moi un MP...
RP et Midi-pyrénées .Km+++ si possibilité de télétravail
Kudos always accepted / Les petits clicks jaunes sont toujours appréciés
Don't forget to valid a good answer / pensez à valider une réponse correcte
Solved!
Go to Solution.
Attachments:
classes methodes.jpg 25 KBHi Tinnitus,
I think you are talking about "inheritance".
Inheritance means that you have a "parent" class and a "child" class.
The "child" is a special type of "parent". Such as a "dog" is a special type of "animal".
The child class has access to all the methods and properties of the parent class.
And the child class can have additionnal methods/properties that the more generic class doesnt have.
It is also possible to overide the methods in the child class to customize them.
For more information about this concept:
LabVIEW Object-Oriented Programming: The
Decisions Behind the Design
http://zone.ni.com/devzone/cda/tut/p/id/3574
In the LabVIEW project explorer, you can go to the properties of a class, and define this class as the child of another class (modify inheritance).
Then, when you instantiate the child class, the object can access the methods/properties of it's parent class.
However, if you instantiate the parent class, the object won't be able to access the methods that are specific to the child class.
The parent class is more generic than the child class.
Hope this will help.
Best regards,
Thomas B. | CLAD
National Instruments France
#adMrkt{text-align: center;font-size:11px; font-weight: bold;} #adMrkt a {text-decoration: none;} #adMrkt a:hover{font-size: 9px;} #adMrkt a span{display: none;} #adMrkt a:hover span{display: block;}
>> Inscrivez-vous gratuitement aux Journées Techniques : de l'acquisition de données au contrôle/com... -
PROBLEM IN ACTIVATION OF A CLASS WHICH IS COPIED FROM STD CLASS
Hi All,
I have a problem in activating a class which is copied from the standard class.
std class: CL_GUI_FRONTEND_SERVICES
The class has got some protected methods and attributes.
here i'm getting following error whenever i try to activate the class:
1) access to protected method "CALL METHOD" is not allowed.
2) access to protected attribute "H_CONTROL" is not allowed.
Thanks & Regards,
Deepakhi,
i am not sure abt d way u hv coded, but there are some basic stuffs wic need to be taken care of:
1: access 2 protected methods are allowed by self class or its inherited classes. it ca't e accessed from outside class. also protected methods can b accessed from FRIEND class. please use inherited class objects to access protected attribute or methods...
2: also i have found that class implementation or declaration or method declaration can't be done inside PERFORM routines... try to avoid using forms.....
more info can be given if i get the exact code..
regards,
ags. -
Inner classes in my interface - ok, no k, indifferent?
I have an interface and a set of ComparatorS which are only used for objects which implement the interface. So I put the ComparatorS in the interface like so (not actual code because it is for my job):
public interface Abc {
//Class loader should only do this once so they are basically singleton constants
public static final Comparator C_1 = new C1();
public static final Comparator C_2 = new C2();
//... there is one more
public String getSomething();
public String getSomethingElse();
//First Comparator inner class
public class C1 implements Comparator {
private C1(){ }
//implements appropriate methods checking for instanceof Abc in the compareTo method
//Compares something (using getSomething())
/* Second Comparator inner class */
public class C2 implements Comparator {
//same thing
//Compares somethingElse (using getSomethingElse())
} //End interfaceUsage:
Set s = new TreeSet(Abc.C_1);
s.add(abc1);Because the Comparator implementations were specific to objects which implement the interface I felt that was the best way to make that fact clear. If they were over in a separate package it would be confusing that these are for the one interface. If they were in the same package as Abc it wouldn't fit (IMO) because they are more utility type classes.
So it was either that or make them a singleton in their own class files or something like this and I found myself liking this.
Any comments on pros/cons of doing it this way?I'm confused as to why it would make sense to put the
Comparators 'in' the interface but not in the package
with the interface. You can't put them 'in' the
interface without putting them in the package. In
reality, nesting them in the interface just creates
classes in the interface's package.My whole deal is that after talking it out I see that it is bad for reuse to have something which could be so generic residing in a particular interface (and therefore deduced soon after that in the same package as my interface). Which made me decide not to have the inner classes because it IS in the same package at that point. But I am saying that is bad. As I tried to state before, for the same reason I now think it is bad to have the classes in the interface (not much i.e. no reuse outside of the interface) then it is bad to have it in the same package as the interface (unelegant reuse outside of the package). Because if the getSomething() is getName() that could be used as a comparator for many different objects in different packages. I don't want to limit. This stance was a progression, once I decided not in the interface, I then also decided not in the same package.
Java is strongly typed. If the class isn't an
instance of the class you are casting to (in this
case the interface) it doesn't matter if it has a
method with the same signature. The cast will fail.Yes, of course, but like generics, you are not limited to just one type (Object is the type of the parameter). So I can either use instanceof before casting (not the best way) or I could have private overloads of the compareTo() method, a new one for every type I support and each supported type which has a getName() could come from a couple different packages. Not that generics does it the same way but on top of being about to declare hierachies that a genericized object can deal with you can also have comma delimitted other objects. They don't have to be related by package or hierarchy in generics. So I don't think it would be so bad for my Comparator to be (if it fit that these different types all had the same method needed by my Comparator).
>>
Here's anothe reason not to do that. Let's say that
someone is using the interface. They need to compare
it to something else, if the Comparators are right
there with the interface, they will be likely to find
them. If they are in some other package, they'd have
to know where to go look for them.If that is the case, where I was worried about easy to find then I would go back to using inner classes with each Comparator as a constant. If I was worried about ease of finding where these ComparatorS are over code reuse across packages.
I mean, I think if I am going to tie the ComparatorS so close to a particular Type then I am going to add it as an inner class whether my type was an interface or a class. At least that is what I am thinking now.
>
This is what I am talking about. So many Java
developers treat packages like filing cabinets. I
put things that do this here and things that do that
there. But this is actually not very useful and it
hamstrings your designs.
Java doesn't have a friend class syntax like C++. So
there's no way to say this class over here has access
to this but no one else does. These kinds of
relationships are very desireable. Some classes will
need to access things in another class that shouldn't
be public. You can do this cleanly if they ae in the
same package but not otherwise.But I am not talking about taking an abstract class putting it in pack1 an a subclass in pack2. I am talking about putting a utility class that could eventially work on 50 classes in a place that is playingfield neutral and not in the original type it deals with today only. I am dealing with public accessor methods on my objects.
>
For example. Let's say you want to create a Factory
(I'm using Factory in a loose way here not
necessarily a GoF Pattern) for a public class. You
don't want every class to have access to contructor
because it creates the Object without initializing
all the necessary atttributes. The factory is
specially crafted to do this with protected info and
access rights. If you put the class and the factory
together it;s a snap. You create a package-protected
constructor, some package-protected methods and/or
fields and do it. No class outside the package will
be able to do these dangerous operations. How do you
do this if you have a factory package and a data
object package?That is a whole nother situation I think. In that case it is obvious. I thought my situation was a little less obvious because of the utility nature of the classes (ComparatorS) that I was dealing with. -
Template as template parameters and friend
Hi,
The following code does not compile with "Sun C++ 5.9 SunOS_sparc Patch 124863-03 2008/03/12". Should be a compiler problem, or?
Thanks in advance!
template <class T>
class A
template <template<class> class T, class E>
class B
friend class T<E>;
void f()
B<A, int> b;
}I believe the code is valid, and we have a compiler bug. Please file a bug report on the C++ compiler at
[http://bugs.sun.com] -
Proper object oriented design for factory class
I have a factory class (UserFactory) it is capable of creating two types of classes (Customer and Employee) that extend User. User is an abstract class.
At present any class could create a Customer of an Employee.
How do I restrict access to the constructors of Customer and Employee only to UserFactory?
I don't want to make both of these classes interfaces (in which case I could just have UserFactory implement both of them).
Here is an example of how they are set up at present:
public abstract class User {
public class Customer extends User{
public Customer{
public class Employee extends User {
public Employee {
public class UserFactory ???? {
private static UserFactory c_userFactory = null;
private UserFactory (){
public UserFactory getInstance(){
//private clone method too
public Customer createCustomer(){
public Employee createEmployee(){
Oh and I don't want to create two separate factory classes for Employee and Customer. In C++ there is a concept of friend classes that would have worked nicely in this case.
Thanks for any and all suggestions,
TimSo correct me if I'm wrong but your suggestion would be something along the lines of:
public abstract class User {
public class Customer extends User{
protected Customer(){
public class Employee extends User {
protected Employee (){
public class UserFactory ???? {
private static UserFactory c_userFactory = null;
private UserFactory (){
public UserFactory getInstance(){
//private clone method too
public Customer createCustomer(){
public Employee createEmployee(){
public class AccessForCustomer extends Customer {
public class AccessForEmployee extends Employee {
Is the above about what you were talking about? I haven't used inner classes before, so I'm a bit curious. Does this mean no class other than UserFactory or derivatives thereof can access the two classes AccessForCustomer and AccessForEmployee?
Thanks,
Tim -
Reg Class lcl_event_receiver
hi friends
class lcl_event_receiver definition deferred.
In alv sample we have this statement, what is purpose of this statement ?
Exactly what it mean in the program ?
Regards
DevaThis is used so you can refer to this class BEFORE you have actually defined the class. This is particularly useful where you split a program up into a number of includes
program A.
include A_TOP
include A_CL
include A_FORMS
etc.
so if in the A_TOP include you refer to the class in the A_CL you waon't get a syntax error.
for example in A_TOP you have
include <icon>.
field-symbols: <fs> type any,
<fsa>,
<fsb>.
*class lcl_event_handler definition deferred. " Event handler*
then rest of your data
data: go_handler type ref to lcl_event_handler, "<================== syntax error if the definition deferred stmt missing
etc
The class is in your include A_CL
class lcl_event_handler definition.
* add other methods etc here at will depending on the
* application --such as new buttons on toolbars etc.
public section.
methods:
handle_double_click for event double_click of cl_gui_alv_grid
importing
e_row
e_column
es_row_no
sender,
etc etc.
cheers
jimbo
Maybe you are looking for
-
Material Stock at storage location for particular date!
Dear Friend, I need a table or bapi which gives stock for particular storage location on particular day. That is MMBE and if you go into detail list (shift+f7). I want same thing. I have tried in MARD table, but it gives stock of a material for most
-
crawler create error help me~~ ====================================================== 2315 portal.doostg01.Administrator 8-1-2007 15:22:06.539 Debug OpenKernel.OpenHttp.Network .NET #293 com.plumtree.openkernel.impl.openh
-
History won't consistently work
For some reason, every time I use a Google search Safari won't put it in the history menu. I debated whether this was Safari's or Google's fault, and I believe it is Safari's (I doubt Google has control over my history). If this is a "new feature," t
-
Hide preferences button in LaunchPad BO4
I need to hide the preferences button in launchpad for BO4. Noticed that in BO3, I can hide it thru CMC/Applications/Infoview. but in Bo4, can find it. Thanks.
-
Importing video from flash card
I tried import some short video i took from a cannon digital camera.the still shoots imported fine into iphoto,but i cant seem to view the video.quick time does not recognize it.is there a soloution?thanks