Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

access modifiers

Thread view: 
man4*.* - 18 Nov 2006 12:42 GMT
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?
MW - 18 Nov 2006 13:16 GMT
> code example:
>
[quoted text clipped - 16 lines]
>
> 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.

Signature

Klingon function calls do not have 'parameters' - they have 'arguments'
- and they always in them.

man4*.* - 19 Nov 2006 00:41 GMT
> 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?
Patricia Shanahan - 19 Nov 2006 02:11 GMT
> code example:
>
[quoted text clipped - 9 lines]
> 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
[quoted text clipped - 3 lines]
>
> 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
man4*.* - 19 Nov 2006 08:46 GMT
> 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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.