• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

protected method access and inheritence

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following piece of code:



I expected I to be OK and III to fail, but I didn't expect the failure of II. Does this mean the compiler looks at the type (Foo) not at the class the object referenced by 'f' belongs too(Bar)?

Can anyone shed some light on this matter?
[ April 15, 2006: Message edited by: Bud Fox ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep ! looks at whether the function is referenced by a foo or by a bar.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case II is failing because you declared incorrectly the call to the method. Your variables is fb, but you're calling the method f.func().

Fix this and it will work.


Regarding the protected access read JLS section 6.6.2 under Details on protected Access.

Best regards,
Edwin Dalorzo
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Dalorzo:
Case II is failing because you declared incorrectly the call to the method. Your variables is fb, but you're calling the method f.func().

Fix this and it will work.

Sorry about that silly typo. It still does not work when chaning it to the intended 'fb'.


When reading the jls section you suggested (6.6.2) I get even more confused, as it says; "A protected member [..] 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."

Is the main method in the example I gave earlier responsible for the implementation? I don't think so..

[ April 15, 2006: Message edited by: Bud Fox ]
[ April 15, 2006: Message edited by: Bud Fox ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right. It does not run. It is just that I changed a bit your code when I did some refactoring in my editor.

Well, the point is that your main method just created a new Foo object and is trying to access its func() method, which is protected. That is not allowed, but you are allowed to access the Bar class own implemenetation or inherited method func()

For instance







For another explanation see section 6.6.7 of the JLS under Examples: protected Fiels, Methods, and Constructors.

I hope this helps.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bud, is that your real name? Do people ask about stock information?

Mark
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
Hey Bud, is that your real name? Do people ask about stock information?

Mark


'American Express' got a hit man lookin' for me.'

Back ontopic; The protected access modifier is often talked about as if it is simple but it turns out to be pretty complex and not very well understood! Therefore, I'll investigate this topic (read the chapter in jls and whatever I run into dealing with this subject) and post a (hopefully) correct explanation when I get to it.
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bud Fox:

'American Express' got a hit man lookin' for me.'



But you didn't answer my question with a yes or a no. So, I will assume that that means that the answer is no.

We have a naming policy here at JavaRanch where we require a real first and real last name. And you name is a fictitous character from the move "Wall Street". Accounts that fail to comply with the naming policy will be removed. You can change your display name by clicking the "My Profile" link above.

Mark

Mark
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:

We have a naming policy here at JavaRanch where we require a real first and real last name. And you name is a fictitous character from the move "Wall Street". Accounts that fail to comply with the naming policy will be removed. You can change your display name by clicking the "My Profile" link above.


I'm sorry. I wasn't aware of the naming policy and I haveve changed my display name to my real name.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
According to me if there is protected identifier than it will become class own method after inheritance on other Package because procteted member are visible by inheritance on other pasckage.
as in this case the packages are different.
Now any access to this method can be done by the reference of the sublass only.
i II the reference is of parent type and the compiler will reject it because it donot allow the parent reference for protected member.
 
Crusading Chameleon likes the size of this ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic