I have a property in a base class that I don’t want overridden for any reason. It assigns an ID to the class for use with a ThreadQueue I created. I see no reason whatsoever for anyone to override it. I was wondering how I can block anyone from attempting to override it short of them changing its modifier.
private int _threadHostID = 0;
public int ThreadHostID
{
get
{
if (_threadHostID == 0)
{
_threadHostID = ThreadQueue.RequestHostID();
}
return _threadHostID;
}
}
Edit: totally forgot the language: C#.
Edit2: It is not virtual or overriding anything else so please no sealed.
There is no way to stop member hiding. If you don’t make it virtual or abstract, then a derived class cannot override it properly anyway, hiding isn’t polymorphic.
If a derived class hides it using the
newoperator, then they are opening up problems for themselves as any code that decides to use a reference to the base class will not touch the derived member. So basically, all code that utilises the “base class”-ness of the type hierarchy will bypass all member hiding anyway.The
sealedkeyword only works if a derived type overrides a base type and doesn’t want it to be overridden further… not sure how it plays with thenewoperator though. Most likely the member hiding will still be allowed, but will still have the same direct-type problem.Your task is done by not making the method virtual or abstract, if a person wants to hide members then they are responsible for anything that breaks because they decided to abuse the design.