i have Abstract class
Public class Abstract baseClass
{
public abstract string GetString();
public abstract string GetString1();
}
public class DerivedClass : baseClass
{
public override string GetString()
{
return "test data";
}
public override string GetString1()
{
throw new NotImplementedException();
}
}
In above line of code, i have to implement both abstract method in derived class. But due to some reason i don’t want to implement all methods, just one of them like GetString() only. How can it be done?
Thanks
If
DerivedClassis going to offer common functionality to other classes, you can mark it as abstract, implement one of the methods here, and then inheritors will only have to implement the remaining method.If you aren’t going to support the other method in a given implementation, you still have to expose the method in your class, but similar to what you have here, you would typically throw a
NotSupportedException. Forvoidmethods, you could simply return (do nothing).Finally, if you want to separate out the things that have both methods and those that have only one, you can use interfaces.
You can have one class that implements
IBasePlus, but you can supply this to methods that take a parameter of typeIBase, in which case you won’t see the extra method.