views:

64

answers:

2
class A
{
  public void Foo() {}
}

class B extends A
{
}

class C extends B
{
  public void Foo() {}
}

Does C's Foo() override A's even though B did not override it? Or do I have to add a stub in B that calls the super's method for each one I want to override in C?

+6  A: 

Even though B did not mention it, Foo should still be available to it due to inheritance. By extension, then, Foo is also available to subclass C and should be able to be overridden thanks to polymorphism.

Instances of C, therefore, will use c.foo() (however it is defined), where as instances of A and B will make use of a.foo() because they have not yet been overridden.

Andrew
Presumably then: A a = new C(); a.Foo(); will call C.Foo()?
CShipley
Erm, yes, but that's a rather confusing naming convention for your variables.
Andrew
A: 

Yes C overrides Foo() of A.

This is due to inheritance, though B doesn't override Foo() , Foo() is inherited from A by B.

And as C extends B, Foo() is inherited by C and overriding happens as C defines Foo().

YoK