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?
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: