• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explicit Method Access and Inheritance

 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't a test question but I know how fond people are of test questions so I'll phrase it as such:



This code outputs "Child Child" when run. I'd like it to output "Father Child". Look where it says "PROBLEM LINE". Basically, I have a child method calling a parent method. Within the parent it calls another parent method that is being overwritten by a child method. I'd like it to explicitly call the parent method and ignore the overwritten child. I tried replacing it with Father.this.identify() but no luck.

Catches: can't change access levels (has to all be public).
[ August 06, 2007: Message edited by: Scott Selikoff ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace the body of Father.identify() with a call to a new method Father._internalIdentify(), which contains the current body of Father.identify(). Have Father.save() call _internalIdentify() instead of identify().
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest, that works.

I was hoping there was more of a java-y way to do it. Some access modifier or class tool like calling this/super.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also make identify() private (at least in Parent) or static. That would allow Child to declare its own identify() method without overriding the original , and give the desired output. If that doesn't work for you, there really isn't a simpler way than what EFH suggested.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic