[Logo]
Forums Register Login
classes and methods
I am learning about overloading objects.   I see these words in front of classes and methods showing where it can be seen.  Are they available at both the Class and Method level?  

Private  - The value can only be seen by the class.
Default  -- I am guessing that it is the default value of a variable.  I dont know yet.  I'll probably learn about it later on.
Protected  -- no idea yet
Public  -- The Class/Method can be seen by everyone else.

I have been learning Java with a base of knowledge that I forget and learn over many years.  Maybe I should just start from the beginning.  I don't know.  I only need enough to write Selenium Test Scripts so I might be better off just learning the parts as I go forward.  Please forgive me if I ask things I should know being this far into Java but I"m in a strange learning curve.

Thanks,

Kevin
(1 like)
Access Modifers apply to classes, methods, and fields.

Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package.

Most thing should be specifically identified as private unless otherwise needed directly by other classes.

Protected is like private with the exception that an immediately inherited subclass also has access.
Have a look in the Java™ Tutorials. I think I have given you the right section.

You usually only apply the term overloading to methods and constructors, and it has a very specific meaning. The name is the same but the parameters are different.
 

Carey Brown wrote:Protected is like private with the exception that an immediately inherited subclass also has access.


That seems to imply that not all subclasses have access to protected members, which isn't true. All subclasses can access a protected member because you can't specify a more restrictive access for an inherited member in a subclass. You either keep the same access specified by the superclass or you make it wider.

Can you clarify what you mean by "immediately inherited"?
I also don't think that is correct; protected members are also accessible to all code in the same package. What does protected mean in C++?
An overview:

private: Only code within the same source file can access the member.

default: Only code within the same package can access the member.

protected: Only code within the same package and code inside a subclass can access the member, but when from a subclass, you can't access the inherited member through a reference of the superclass.

public: The member is accessible everywhere. Since Java 9, modules only have access to public members in other modules if the dependency module exports the member.
 

Stephan van Hulst wrote:An overview:
private: Only code within the same source file can access the member.

I don't think so since a source file (a .java file) can have code of another class which doesn't have access to the private member of another class defined in the same source file.
Test.java (a source file)

Rather I would say private members are directly accessed only within the class they are declared in.
My apologies, I had considered this scenario, but I was strongly convinced that you could actually access the private members of top-level classes declared within the same file.

Ganesh Patekar wrote:Rather I would say private members are directly accessed only within the class they are declared in.


This is also not correct. A nested class can access private members of an enclosing class, and an enclosing class can access private members of a nested class.

I guess the correct way to describe private access would be: Only code within the declaring class, or code within enclosing or nested classes of the declaring class can access the private member.
Let's see what the JLS has to say:-

That JLS section, example 6.6.5 wrote:A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.

 
That leaves up to interpretation what is included in the body of the top level class. I'm pretty sure that includes the bodies of nested classes though.
 

Stephan van Hulst wrote:I'm pretty sure that includes the bodies of nested classes though.


Indeed. Everything what is in between of {...} of the top level class.
Well, now that I think more, except static nested classes.
A simple example:
 

Liutauras Vilda wrote:. . . except static nested classes.

The variables are visible within a static nested class, but not accessible because of another rule that code in a static context cannot access instance members. Try it and see what sort of compiler error message you get.
We have broaden discussion boundaries slightly from the original. Actually we were talking about members. Since static aren't members, I'm not sure why I've mentioned them and shot myself to a foot.
 

Stephan van Hulst wrote:an enclosing class can access private members of a nested class.

Agreed I was wrong. I didn't think of private members declared in inner class.
 

Ganesh Patekar wrote:

Stephan van Hulst wrote:an enclosing class can access private members of a nested class.

Agreed I was wrong. I didn't think of private members declared in inner class.


I'm unsure about the wording.

I think nested inner class can access enclosing classes private members, not other way round. But I might misunderstanding the wording.
(0 likes, 2 cows)
 

Liutauras Vilda wrote:I think nested inner class can access enclosing classes private members

Yes 

not other way round.

No, enclosing class can access private members of inner class.

Also private member of inner class is accessible in another inner class (of same enclosing class) which is already stated in Campbell's JLS quote.
Here is an example

But I might misunderstanding the wording.

Yes It is all about wording that's why hats off to those who document JLS.
Enclosing classes may access private members of nested classes and vice versa. That's because all code within the body of a top-level class (including nested classes) may access private members declared anywhere within that same body (including other nested classes).

A great advantage of this is that you can declare nested classes that only expose certain properties to the outside world, and have the enclosing class manipulate their internals as needed. Otherwise the only advantage of having static nested classes at all would be namespacing.

[edit]

Ganesh beat me to the punch.
(1 like)
You can probably even have multiple nested and inner classes which are subtypes of one another or of their enclosing type, allow instantiation only via factory methods, and still return immutable objects. Usually non‑final classes cannot be made immutable, but you can exercise much tighter control of the behaviour of private inner/nested classes.
Thanks all. I don't think I used this stuff much if at all, so got introduced to some new concepts.
Carey
Campbell
Stephan
Ganesh
Liytauras
Piet   and anyone else I missed.  I appreciate you input and discussion.

I did't mean to open a mess here.  I wasn't sure what the modifiers are.  Since I'm new I think I will stick with Public and Private.
I'm using java to work with Selenium.  I probably wont need the other two modifiers for a while.

I am working on an exercise where I need to call a method in the class of another package.  I'm going to start a new topic.

Thanks,

Kevin
Actually, I would press you to learn default access as soon as possible. There's a good reason this access level is the default. If you need to use a member of your class outside your class, give it default access.

People get into a bad habit of making all of their non-private members public. Don't do this, only use public when you really need to.

This thread has been viewed 1272 times.

All times above are in ranch (not your local) time.
The current ranch time is
Nov 17, 2018 10:13:04.