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

redefining inherited fuctions, what gets called then ?

 
Arnaud Burlet
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got :
class A { method f() defined }
class B extends A {method g() and method f() defined again }
class C extends B {method f() defined again }

Now which method gets called when from method g() (class B) I call:
f() ?
this.f() ?

super.f() will of course call A.f()

thanks, Arnaud
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either B.f or C.f, depending on which class the object you call g on is an instance of.
 
Arnaud Burlet
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I have to be more precise, sorry...

If I have : toto instaceof C
and I call : toto.g()

both call to f() and this.f() from method g() will call f() from C ?

any way to call f() from B ? cast "this" to B ? ((B) this).f() ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnaud Burlet:

any way to call f() from B ?


C.f() could call B.f() using super.f(), but there's no way for B to say "use the f() in B, and only in B." If B needs to do that, then the following is standard practice:


[ October 13, 2004: Message edited by: Ernest Friedman-Hill ]
 
Nigel Browne
Ranch Hand
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused as to what your code is trying to achieve but if you want to call the f() method in class B followed by the f() method in class C then the following code shows you how
 
Arnaud Burlet
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thank you both!

It was just a theoretical question, I saw some piece of code where G() in B was calling f(), and I wanted to know more about that behaviour...

What I wanted to know was :
there's no way for B to say "use the f() in B, and only in B."


thread end.

Arnaud
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B could declare f as final and therefore prevent C from overriding it...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic