why a class can't access protected method from another class in thesame package,the method is interi

J

junzhang1983

package XX;
class A
{
protected void kk ();
}

package YY;
import XX.A;

class B extends A
{

}

package YY;
class C
{
public void zz()
{
kk(); //why can call B.kk()????
}
}



but if we modify as below:
package YY;
import XX.A;

class B extends A
{
protected void kk ()
{
super.kk();
}
}


why above modify can success?
3ks
 
H

Hal Rosser

package XX;
class A
{
protected void kk ();
}

package YY;
import XX.A;

class B extends A
{

}

package YY;
class C
{
public void zz()
{
kk(); //why can call B.kk()????
}
}
_________
Because protected keyword gives access to subclasses and other members of
the same package.
And class C is not a subclass and not in package XX.
but if we modify as below:
package YY;
import XX.A;

class B extends A
{
protected void kk ()
{
super.kk();
}
}


why above modify can success?
3ks
Because B is a subclass of A
 
L

Lew

This will not compile. Why does the comment refer to B.kk()? Class B is not
involved in the C invocation at all. C does not have a method kk().
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top