I have a base class Foo that is concrete and contains 30 methods which are relevant to its subclasses.
Now I've come across a situation that is specific only to the base class,and I want to create a method that cannot be inherited, is this possible?
Class Foo
{
/* ... inheritable methods ... */
/* non-inheritable method */
public bool FooSpecificMethod()
{
return true;
}
}
Class Bar : Foo
{
/* Bar specific methods */
}
var bar = new Bar();
bar.FooSpecificMethod(); /* is there any way to get this to throw compiler error */
EDIT
I'm not sure if I was clear originally.
I do understand the principles of inheritance, and I understand the Liskov substitution principle. In this case there is a single exception that ONLY deals with the 'un-inherited' case, and so I did not want to create an 'uninheritedFoo' subclass.
I was asking if it is technically possible to create a situation where foo.FooSpecificMethod() is a valid and publicly accessible method, but subclassoffoo.FooSpecificMethod() throws a compiler error.
Essentially I want a sealed method on an unsealed class.