开发者

Overriding a method in the superclass' superclass in java?

开发者 https://www.devze.com 2023-01-15 07:59 出处:网络
class A { public void Foo() {} } class B extends A { } class C extends B { public void Foo(开发者_如何学编程) {}
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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消