File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Method overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method overriding" Watch "Method overriding" New topic
Author

Method overriding

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
I have a method orThis(), which is present in class TopTier.

This method in inherited in class MiddleTier, when MiddleTier extends TopTier. Now in MddleTier class , I am overriding orThis(). But If I want to use the behavior of orThis() available in TopTier class, I can still use super.orThis() to invoke orThis available in TopTier class.

Now , I am creating BottomTier class, which extends MiddleTier class. And again orThis() is overridden in BottomTier class. Now if I use super.orThis() in BottomTier, will it call orThis() from MiddleTier class or TopTier class. I think it should call orThis() from MiddleTier as its immediate super class.

If this is true, then in the instance of a BottomTier class , how to invoke behavior of orThis() available in TopTier super class ?

Thanks

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

You are correct it will call the middle tier class. You could expose a helper method in middle tier to call the top tier's class.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
Thanks.

I did not understand your reply.
If I want to invoke orThis() available in the TopTier class in the BottomTier class, how can I do this ?
BottomTier is sub class of MiddleTier, which in turn is subclass of TopTier. orThis() , which is in TopTier class is overridden in sub class MiddleTier and again orThis() is overriddden in BotomTier which is sub class of MiddleTier. I want to call orThis() in BottomTier class which is available in super class of super class.

Thanks.
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 117
What Jeanne means is having a method in MiddleTier class which will just return super.orThis().


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

This is very frequently asked interview question!

How do you call TopTier method if you do not have access to change super classes TopTier and MiddleTier?


SCJP 5, SCWCD 5
nimish kumar
Ranch Hand

Joined: Dec 04, 2009
Posts: 56
Prabhakar Bokka wrote:This is very frequently asked interview question!

How do you call TopTier method if you do not have access to change super classes TopTier and MiddleTier?


I just tried. Without changing TopTier and MiddleTier, we can access the method in TopTier from BottomTier by instantiating the TopTier in BottomTier and calling that method.


IBM-IBM WebSphere Application Server Network Deployment V7.0, Core Administration.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

But that may not always be an option. You are creating a new instance of a TopTier class, with new member variables. the values those are set to may not be the same...or it may change something in the newly created TopTier instance, when what you really need is changes to your BottomTier instance.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
nimish kumar
Ranch Hand

Joined: Dec 04, 2009
Posts: 56
fred rosenberger wrote:But that may not always be an option. You are creating a new instance of a TopTier class, with new member variables. the values those are set to may not be the same...or it may change something in the newly created TopTier instance, when what you really need is changes to your BottomTier instance.


hey, its so simple. We make our BottomTier extend TopTier. Then call super.isThis().
But, again here we are compromising on the MiddleTier's methods. BottomTier wont be able to access MiddleTier's members.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Friends!

I got the answer for this. using below command we can access TopTier method in BottomTier without using instances.

((TopTier)this).orThis();
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That is an interesting question.

A similar one can be found here.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Prabhakar Bokka wrote:
I got the answer for this. using below command we can access TopTier method in BottomTier without using instances.

((TopTier)this).orThis();


This won't work if the method is overridden, as specified in this topic. With overridden methods, all this will do is call the latest version, which is the bottomtier version.

This will only work if the method is not actually overridden, but hidden, such as when the method is private to the toptier class, or if the method is static.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
This won't work if the method is overridden, as specified in this topic. With overridden methods, all this will do is call the latest version, which is the bottomtier version.

This will only work if the method is not actually overridden, but hidden, such as when the method is private to the toptier class, or if the method is static.


Henry, This is only half answer. How would you call TopTier class method in the BottomTier class , which is subclass of MiddleTier which in turn is subclass of TopTier.

orThis() method is overridden in MiddleTier class and BottomTier class.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
If that is an interview question, it is a bit like asking an applicant for a driving job whether he knows how to do a handbrake turn.

Accessing the super-superclass breaches the laws of inheritance, because you no longer have "the subclass object IS-A superclass object."
 
 
subject: Method overriding