Core java question

Suppose we have three classes X, Y, and Z

1.X has a public method f()

2.Y extends X and overrides f()

3.Z extends Y

4.x is an object of type X, y is an object of type Y and

z is an object of type Z.

when I invoke z.f(), which version of f() will be called?

The one in Y, i.e. y.f() or x.f()?

I presume that it will be y.f(). Could anyone tell me why y.f() will be invoked. I need to know the concept behind this.

Appreciate a speedy reply for this.

Message was edited by:

RRaghavendra

[561 byte] By [bronze-starDukes] at [2007-11-26 12:15:04]
# 1
because Z extends Yand all the methods of Y are inherited
bronzestar at 2007-7-7 14:18:28 > top of Java-index,Archived Forums,Socket Programming...
# 2
You provided the answer yourself: Y overrides f() , and Z extends Y. So Z can only see the f() defined in Y, even if you explicitly cast it to type X.
bronzestar at 2007-7-7 14:18:28 > top of Java-index,Archived Forums,Socket Programming...
# 3
Thanks Peetzore!!!That's the answer I was looking at !null
bronzestar at 2007-7-7 14:18:28 > top of Java-index,Archived Forums,Socket Programming...