aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Levels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access Levels" Watch "Access Levels" New topic
Author

Access Levels

Ashoka Jayasekara
Greenhorn

Joined: Feb 01, 2011
Posts: 12


Can any one explain the 3rd result with the access modifiers.


[HENRY: Added code tags]
Shaikh Ali
Ranch Hand

Joined: Jan 26, 2011
Posts: 51
You can only access A's m() from non-static class methods of class B.
Ashoka Jayasekara
Greenhorn

Joined: Feb 01, 2011
Posts: 12
Faraz Kadri wrote:You can only access A's m() from non-static class methods of class B.

I tried with non static methods. But i can not access it Via non static methods in B. These two classes are in two packages. please consider that with the Access Modifiers.
Shaikh Ali
Ranch Hand

Joined: Jan 26, 2011
Posts: 51
Ashoka Jayasekara wrote:
Faraz Kadri wrote:You can only access A's m() from non-static class methods of class B.

I tried with non static methods. But i can not access it Via non static methods in B. These two classes are in two packages. please consider that with the Access Modifiers.


Imagine the following method in your class B.



You can only access protected method using the first form, but not the second.

[HENRY: added code tags]
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

protected method can only be accessed by subclass e..g when Type of variable is subclass or inside subclass, outside package.

http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Ausan Abdulhabib
Ranch Hand

Joined: Apr 25, 2010
Posts: 65
As Javin said,

protected method can only accessed out side the package through inheritance. i.e. by making sub class.
Hope this clear to you.


OCPJP/SCJP 6.0
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Ausan Habib wrote:As Javin said,

protected method can only accessed out side the package through inheritance. i.e. by making sub class.
Hope this clear to you.


I'm getting a bit confused by this... I am very well aware of the protected access modifier only allowing access through inheritance, ie: by calling the subclass's own (inherited) method. However...

This is an instance method we are talking about, and the fact is the instance in question is an instance of B, while ofcourse its ref variable is of type A... I'm still getting a bit confused here because if I compare this with a static class method, which cannot be overridden only "redefined", then yes the ref var type is the deciding factor for which static method gets executed...

But here we are talking about an instance method, one which belongs to an object which in fact we can execute the method on... My question becomes, shouldn't the runtime object's access modifier of the method in question be the deciding factor, as opposed to the reference variable type's access modifier, which seems to be the case?

Appearently it's not. Casting the reference variable to a B-type, makes it possible to invoke the method, but the cast appears to be necessary... I just find this a little odd, since once again we are talking about an instance method that has been overridden and I feel like there should be made a runtime check of the access modifier.

Just so there are no confusions, here's the type of code im referring to:





// Andreas
Ausan Abdulhabib
Ranch Hand

Joined: Apr 25, 2010
Posts: 65
A z = new B();
((B)z).m(); // works fine, but not without the cast...

You must make a downcast because during compile time, the compiler look into reference type, so in our case it does not see the m() method as it has protected access, which means it can gain the access through inheritance only, not through make new instance by using new.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Access Levels