This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overriding

 
nirjari patel
Ranch Hand
Posts: 386
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nirjari patel
Ranch Hand
Posts: 386
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Jeanne means is having a method in MiddleTier class which will just return super.orThis().
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 196
Java Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
nimish kumar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimish kumar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
Java Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an interesting question.

A similar one can be found here.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
nirjari patel
Ranch Hand
Posts: 386
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47261
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic