aspose file tools*
The moose likes Beginning Java and the fly likes redefining inherited fuctions, what gets called then ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "redefining inherited fuctions, what gets called then ?" Watch "redefining inherited fuctions, what gets called then ?" New topic
Author

redefining inherited fuctions, what gets called then ?

Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
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

Joined: Jul 11, 2001
Posts: 14112
Either B.f or C.f, depending on which class the object you call g on is an instance of.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Oct 08, 2004
Posts: 31
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

Joined: Jul 11, 2001
Posts: 14112
B could declare f as final and therefore prevent C from overriding it...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: redefining inherited fuctions, what gets called then ?