access modifiers

M

man4*.*

code example:

package somethingElse;

public class B {
protected void methodInB() { /*something*/}
}


package certification;
import somethingElse.B;

public class A extends B {
public A {
B newObject = new B();
newObject.methodInB(); //won't compile
methodInB(); //this works
}
}

why I can't acces methodInB over reference?
 
M

MW

man4*.* said:
code example:

package somethingElse;

public class B {
protected void methodInB() { /*something*/}
}


package certification;
import somethingElse.B;

public class A extends B {
public A {
B newObject = new B();
newObject.methodInB(); //won't compile
methodInB(); //this works
}
}

why I can't acces methodInB over reference?
Because the method has access modifier protected. Which means it can be
called from inside the class itself and from any class inside the same
package. Because A is in another package than B, it cannot call the
method inside B.
However it can call the method inside itself.
 
M

man4*.*

Because the method has access modifier protected. Which means it can be
called from inside the class itself and from any class inside the same
package. Because A is in another package than B, it cannot call the

If I follow your definition of protected, then please describe me
default modifier...you say "..and form any class inside the same
package.." ?? such is definition of default modifier? or isn't?

method inside B.
However it can call the method inside itself.

I do not understand why I can't access to the method over
reference when I see the class and use protecded modifier which
allow to access methods in class which is in diferent package...
or did I understand wrong protected modifier?
 
P

Patricia Shanahan

man4*.* said:
code example:

package somethingElse;

public class B {
protected void methodInB() { /*something*/}
}


package certification;
import somethingElse.B;

public class A extends B {
public A {

I think maybe you mean something like "public A() {"
B newObject = new B();
newObject.methodInB(); //won't compile
methodInB(); //this works
}
}

why I can't acces methodInB over reference?

Because you are trying to access the methodInB belonging an actual B,
not an A:

JLS Section 6.6.2 Details on protected Access

"A protected member or constructor of an object may be accessed from
outside the package in which it is declared only by code that is
responsible for the implementation of that object."

http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#62587

The plain methodInB call accesses the method methodInB for an A instance
from code responsible for implementation of class A objects, so it is
fine. On the other hand, the newObject.methodInB call attempts to access
the methodInB in a B object, from code that is not responsible for the
implementation of class B.

Patricia
 
M

man4*.*

The plain methodInB call accesses the method methodInB for an A instance
from code responsible for implementation of class A objects, so it is
fine. On the other hand, the newObject.methodInB call attempts to access
the methodInB in a B object, from code that is not responsible for the
implementation of class B.


As always, your's explanations are amazing and so simple... :))

Thank, thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top