Curtis Shipley
Curtis Shipley

Reputation: 8090

Overriding a method in the superclass' superclass in java?

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?

Upvotes: 1

Views: 244

Answers (2)

YoK
YoK

Reputation: 14505

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().

Upvotes: 0

Andy
Andy

Reputation: 3170

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.

Upvotes: 6

Related Questions