#69 Virtual override

brian Tue 23 May 2006

Consider this case:

class A 
{ 
  virtual Void a() {} 
}

class B extends A
{ 
  override Void a() {} 
}

Do you think B.a() is both an overriden AND virtual - that is subclasses of B can override a() implicitly without declaring a() to be both virtual and override. The real issue is that we don't have a notion of "final" since we are going the opposite route and making things virtual. My inclination is to say that B.a() is NOT virtual (in Java speak it is implied to be final regardless of the fact that it overrides a virtual method from a base class).

andy Tue 23 May 2006

If I follow, I think virtual methods are always virtual - and override does imply virtual - no need to say virtual override. However, I think we probably do need some mechanism to control that downstream - with maybe a final keyword. So in your case above we could still do this:

class C extends B
{
  override Void a() {}
}

Which seems natural to me. But if I wanted to dissallow that, I could have said something like this:

class B extends A
{
  final override Void a() {} // ???
}

That is an interesting comment though, I am not sure how it should work.

brian Tue 23 May 2006

No you have it backward - override does NOT imply virtual - so you need to explicitly specify virtual for methods you with a subclass to override.

John how does C# do it?

andy Wed 24 May 2006

I think override implying virtual seems more natural - why do you think it should default to final?

john Wed 24 May 2006

I think B.a() is not virtual. If I don't declare it to be virtual then it's final. I need to check and see if virtual in a base class is inherited by subclasses in C#. I don't think it is.

brian Thu 25 May 2006

We should do it however C# does it

brian Sun 2 Dec 2007

A couple months ago I changed the compiler such that the override keyword implies virtual (in fact it is an error to use both). The result of that change was that there wasn't a mechanism to declare an override, but prevent subclasses from overriding. So I added support to the compiler for a "final override" when those two keywords are used together. Final isn't technically a modifier which can be used for slots, and isn't available in the reflection APIs - in this case final is just used as a way to turn off the virtual flag.

Login or Signup to reply.