access levels in inheritance

Joined
Jan 17, 2010
Messages
2
Reaction score
0
when runing following code, I get:


package a;
public class Base
{
protected void pro()
{
System.out.println("This is a protected method.");
}
}

package b;
import a.Base;
public class Derived extends Base
{
public static void main(String[] args)
{
Derived dr = new Derived();
dr.pro();//correct
}
}

package b;
public class Another
{
public static void main(String[] args)
{
Derived dr = new Derived();
dr.pro(); //error
}
}

package a;
import b.Derived;
public class Another2
{
public static void main(String[] args)
{
Derived dr = new Derived();
dr.pro();//again correct
}
}


I was wondering why the result was that. Then I drew a picture showing the behavior of the protected method of the base class.

i3.6.cn/cvbnm/c7/15/37/9b5fe4d9938fc79b0a540ee702287c55.jpg
Is my understanding on this matter correct?


The following shows the default access modifier:

package a;
public class Base
{
void def()
{
System.out.println("The default mothod of the base class.");
}
}


package a;
public class Derived extends Base
{
public static void main(String[] args)
{
Derived dr = new Derived();
dr.def();//*****correct*******
}
}

package b;
import a.Base;
public class Derived2 extends Base
{
public static void main(String[] args)
{
Derived2 dr2 = new Derived2();
dr2.def();//********error*************
}
}


package a;
public class Another
{
public static void main(String[] args)
{
Derived dr3 = new Derived();
dr3.def();//************correct**********
}
}


Again I drew pictures.
Look!

i3.6.cn/cvbnm/31/b4/37/961f5e59bcb8848c796528263331a74e.jpg
i3.6.cn/cvbnm/ba/b3/f2/7cfa99d3a70997f4fd2788196cf23366.jpg

Are there really an inner interface and an outer interface for a member method of base class?
 
Last edited:
Joined
Jan 17, 2010
Messages
2
Reaction score
0
Oh! How stupid I am.
Those so called objects don't even exist.
Object is only a way to bind data and methods together.
 

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