473,783 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

public vs private access, quick question-

Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method() ;

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff
Nov 16 '05 #1
6 3591

"z_learning_tes ter" <so*****@micros oft.com> wrote in message
news:RIPBc.8815 7$HG.75569@attb i_s53...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

There are no private classes(atleast , not at top level, nested classes can
be private). For non-nested classes(whats the term one would use here,
anyway?) there are only two accessbilities: public and internal. A class
without a specifier is internal and accessible to every other class in that
assembly.
int myVal = class1.method() ;

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff

Nov 16 '05 #2
Great, just what I needed.
Thanks!

Jeff

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:uN******** *****@TK2MSFTNG P11.phx.gbl...

"z_learning_tes ter" <so*****@micros oft.com> wrote in message
news:RIPBc.8815 7$HG.75569@attb i_s53...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

There are no private classes(atleast , not at top level, nested classes can
be private). For non-nested classes(whats the term one would use here,
anyway?) there are only two accessbilities: public and internal. A class
without a specifier is internal and accessible to every other class in

that assembly.
int myVal = class1.method() ;

It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff


Nov 16 '05 #3
Jeff,

Remember that private classes can only be declared INSIDE another
class. The private class member would only be accessible by the class
that it is inside, and in turn, the static public method as well. Here
is a quick example:

public class MainClass
{
public MainClass()
{
// you can call the static public method from here
MyPrivateClass. TestMethod();
}

// private classes can only be declared inside another class
private class MyPrivateClass
{
public MyPrivateClass( )
{
// it works here as well of course
TestMethod();
}

public static void TestMethod()
{
}
}
}

public class AnotherClass
{
public AnotherClass()
{
// it doesn't work here, MyPrivateClass isn't accessible
MainClass.MyPri vateClass.TestM ethod();
}
}

Hope that clears things up.

"z_learning_tes ter" <so*****@micros oft.com> wrote in message news:<RIPBc.881 57$HG.75569@att bi_s53>...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method() ;

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff

Nov 16 '05 #4
Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public
MyPrivateClass( )"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are
always public even if their class is private, though it seems like a
contradiction.. .

Oops, then another question- you are calling the method "TestMethod ()" from
inside the constructor.
Can you do that?
All I have seen so far inside constructors is variable initializations .
Again, my book is bad. Very bad.

Thanks!

Jeff

"Eli Block" <eb****@gmail.c om> wrote in message
news:76******** *************** **@posting.goog le.com...
Jeff,

Remember that private classes can only be declared INSIDE another
class. The private class member would only be accessible by the class
that it is inside, and in turn, the static public method as well. Here
is a quick example:

public class MainClass
{
public MainClass()
{
// you can call the static public method from here
MyPrivateClass. TestMethod();
}

// private classes can only be declared inside another class
private class MyPrivateClass
{
public MyPrivateClass( )
{
// it works here as well of course
TestMethod();
}

public static void TestMethod()
{
}
}
}

public class AnotherClass
{
public AnotherClass()
{
// it doesn't work here, MyPrivateClass isn't accessible
MainClass.MyPri vateClass.TestM ethod();
}
}

Hope that clears things up.

"z_learning_tes ter" <so*****@micros oft.com> wrote in message

news:<RIPBc.881 57$HG.75569@att bi_s53>...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method() ;

It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff

Nov 16 '05 #5
z_learning_test er <so*****@micros oft.com> wrote:
Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public
MyPrivateClass( )"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are
always public even if their class is private, though it seems like a
contradiction.. .
No, constructors aren't always public. You can have private
constructors which would only be able to be called within the class. If
a private class has a public (or internal) constructor, then the
enclosing class will be able to call it.
Oops, then another question- you are calling the method "TestMethod ()" from
inside the constructor.
Can you do that?


You certainly can. It's generally a bad idea to call *virtual* methods
inside constructors though.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Well, I'm definitely learning more than just one 'something new' every day
;-)
Thanks again!

Jeff
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
z_learning_test er <so*****@micros oft.com> wrote:
Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public MyPrivateClass( )"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are always public even if their class is private, though it seems like a
contradiction.. .


No, constructors aren't always public. You can have private
constructors which would only be able to be called within the class. If
a private class has a public (or internal) constructor, then the
enclosing class will be able to call it.
Oops, then another question- you are calling the method "TestMethod ()" from inside the constructor.
Can you do that?


You certainly can. It's generally a bad idea to call *virtual* methods
inside constructors though.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2323
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
10
3681
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global' variable for the application. Any other code within the application can access it. public - can be shared across the application if instatiated. Does that sound about right? It seems these are more useful for methods rather than variables. Most of...
27
2718
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. What is the normal way to do this? Thanks, Thomas --
8
3954
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For example: class A { public: const vector<int>& getTable() { return m_table; }
2
2441
by: Sky | last post by:
Hello: I'm trying to make sense of snk files, when to use, under what conditions to regenerate new ones,...can someone take a look if these statemes make sense? And then the final questions at the end that they first statements bring up in my mind... a) Because two developers, unbeknownst to each other, can end up releaseing different dll's with the same name, one should sign an assembly with a unique tag. right?
9
2359
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice = CDec(txbInv.Text) Wage = CDec(txbTotWage.Text)
6
1840
by: Ajay Martin | last post by:
Why would it be reasonable for someone to argue that it is incorrect to allow a public member inherited from a public base class to be redefined as private?
23
2653
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class Add_Molecule_Req: public Action_Request{ // ...... };
2
1783
by: fgh.vbn.rty | last post by:
Hi, I'm not sure if i'm asking the question correctly but anyway here it is. Say I have 3 classes - class A, class B, class R. 1) A and B are the building blocks and R is like a repository that stores objects of A and B. 2) A is at the lowest level and should "know about" only other As. B should know only about As and other Bs.
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.