wood burning stoves*
The moose likes Java in General and the fly likes Explicit Method Access and Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Explicit Method Access and Inheritance" Watch "Explicit Method Access and Inheritance" New topic
Author

Explicit Method Access and Inheritance

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3702
    
    5

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: 24183
    
  34

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: 3702
    
    5

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: 18671
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Explicit Method Access and Inheritance
 
Similar Threads
Doubt in equals method
Strange "this" behavior
Doubt in overloading & overriding
Problem -- Virtual Method Invocation
Constructor,why displaying null on this program