• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Levels

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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


[HENRY: Added code tags]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only access A's m() from non-static class methods of class B.
 
Ashoka Jayasekara
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected method can only be accessed by subclass e..g when Type of variable is subclass or inside subclass, outside package.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic