| Author |
Explicit Method Access and Inheritance
|
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
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 ]
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
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().
|
[Jess in Action][AskingGoodQuestions]
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
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
Joined: Jan 30, 2000
Posts: 18670
|
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: Explicit Method Access and Inheritance
|
|
|