File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Explicit Method Access and Inheritance" Watch "Explicit Method Access and Inheritance" New topic

Explicit Method Access and Inheritance

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3749

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 ]

[OCA 8 Book] [Blog]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Replace the body of Father.identify() with a call to a new method Father._internalIdentify(), which contains the current body of Father.identify(). Have call _internalIdentify() instead of identify().

[Jess in Action][AskingGoodQuestions]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3749

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

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
It's not a secret anymore!