NotInheritable Classes


N

Nick Hall

I've come across what seems to be a compiler problem in the following
scenario. Suppose we have a base class which implements an interface:-

Interface IDemoInterface
ReadOnly Property Flag() As Boolean
End Interface

Public Class BaseClass
Implements IDemoInterface

Protected Overridable ReadOnly Property BaseFlag() As Boolean Implements
IDemoInterface.Flag
Get
Return True
End Get
End Property
End Class

I don't what the property exposed as part of the public interface of the
class and I do want deriving classes to be able to override it if they want.
If I define the deriving class like this: -

Public NotInheritable Class DerivedClass
Inherits BaseClass

Protected Overrides ReadOnly Property BaseFlag() As Boolean
Get
Return False
End Get
End Property
End Class

The compiler shows the following error message: -

'NotInheritable' classes cannot have members declared 'Protected'.

Now I can see that it makes sense to prevent NotInheritable classes defining
Protected methods/properties for the most part. However I would have
thought that as I'm overriding a base class property it should be OK.
Futhermore, overriding Protected methods appears to be allowed by the
compiler, so at the very least it seems inconsistent. Removing the
NotInheritable modifier makes all the errors go away (naturally).

Am I missing something fundamental or is this a bug in the VB compiler?

Regards,

Nick Hall
 
Ad

Advertisements

H

Herfried K. Wagner [MVP]

Nick,

Nick Hall said:
I've come across what seems to be a compiler problem in the following
scenario. Suppose we have a base class which implements an interface:-

Interface IDemoInterface
ReadOnly Property Flag() As Boolean
End Interface

Public Class BaseClass
Implements IDemoInterface

Protected Overridable ReadOnly Property BaseFlag() As Boolean Implements
IDemoInterface.Flag
Get
Return True
End Get
End Property
End Class

I don't what the property exposed as part of the public interface of the
class and I do want deriving classes to be able to override it if they want.
If I define the deriving class like this: -

Public NotInheritable Class DerivedClass
Inherits BaseClass

Protected Overrides ReadOnly Property BaseFlag() As Boolean
Get
Return False
End Get
End Property
End Class

The compiler shows the following error message: -

'NotInheritable' classes cannot have members declared 'Protected'.

Now I can see that it makes sense to prevent NotInheritable classes defining
Protected methods/properties for the most part. However I would have
thought that as I'm overriding a base class property it should be OK.

I think the error message is OK, but I would not expect an error message
when I change the modifier to 'Private' ;-).
 
L

Larry Serflaten

Nick Hall said:
I don't what the property exposed as part of the public interface of the
class and I do want deriving classes to be able to override it if they want.

Now I can see that it makes sense to prevent NotInheritable classes defining
Protected methods/properties for the most part. However I would have
thought that as I'm overriding a base class property it should be OK.
Futhermore, overriding Protected methods appears to be allowed by the
compiler, so at the very least it seems inconsistent. Removing the
NotInheritable modifier makes all the errors go away (naturally).

Am I missing something fundamental or is this a bug in the VB compiler?

Keep the NotInheritable, and try:

Shadows ReadOnly Property BaseFlag() As Boolean

That will hide the property in the base class.

You've gotten into a grey area for me as well, be sure to test various
access modes to be sure it will respond as you want....

LFS
 
N

Nick Hall

Thanks for the suggestion.

I think I will probably just not have the NotInheritable attribute on the
class (in reality it's a nested private class so it shouldn't actually hurt
anything). I was just mildly curious as to whether there was any "good"
reasons why it wouldn't work.

The problem was I wanted to override a base class's interface
implementation, not obscure it so I'm not sure the Shadows suggestion would
work in this scenario.

Heh hum, another strange one to add to the list :)

Nick Hall
 
Ad

Advertisements

N

Nick Hall

Comments inline -
Herfried K. Wagner said:
Nick,



I think the error message is OK, but I would not expect an error message
when I change the modifier to 'Private' ;-).

I've got to admit I'm a little curious. Why do you think the error message
is OK? And why (if it does make sense) would it not apply to protected
methods as well?

Just trying to understand things a little better :)

Thanks,

Nick Hall
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top