Sorry for the awful title. I’m sort of a beginner and I wasn’t sure how to properly describe the topic.
If you know a better title after reading the post, please share.
I’m starting to like Interfaces more and more the bigger my project grows.
However, there’s this one thing I haven’t been able to figure out.
I’ll give you my sample code below and describe what I want to do after.
public interface Iinterface
{
void End();
}
public class State : Iinterface
{
public virtual void End()
{
}
}
public class StateSplash : State
{
public override void End() //<- Want to get an error if missing this override method
{
base.End();
}
}
To be honest, I’m not 100% sure what the ‘real’ uses of Interfaces are. But I’m using them just to make sure I’m implementing the methods needed for the class to run properly.
So when I’m editing the base State class, I’ll get a compile error if I’m missing a method from the Interface.
That’s great!
However, I want to get the same type of error if the StateSplash class is missing an override of the same method.
I assume there’s a way to do this.
is ‘State’ a concrete class? What I mean is that can there ever be a ‘State’ object (rather than a StateSplash)?
If no, then you can flag the class as ‘abstract’. This says that it’s a class that must be inherited from to be used, you can never have an instance of just it… you only have instances of subclasses of it.
Then in an abstract class you can have abstract methods. Methods that must be implemented in child classes.
public interface Iinterface
{
void End();
}
public abstract class State : Iinterface
{
public abstract void End()
}
public class StateSplash : State
{
public override void End() //<- will throw error if this doesn't exist
{
//you never call base.End() because base.End() is abstract... there's no code in it
}
}
Note, this assumes that State.End never has code in it, and it’s up to the StateSplash (or other child class) to create code.
Where as if State is concrete, or State.End has code in it… well, then there’s no way to force this. If it is abstract, and has code in it, you could create an alternate ‘DoEnd’ method (or something) that is abstract and StateSplash must override that instead.
There is no way to directly do what you’re seeking, while keeping the State class non-abstract.
If you’re willing to make the State class an abstract class, then you can change the End() method to be an abstract method, which would require it to be implemented by children.
So after some quick reading and testing. I ended up with the following:
public abstract class State
{
protected abstract void End();
protected virtual void OnEnd()
{
}
}
public class StateSplash : State
{
protected override void End()
{
base.OnEnd();
}
}
I’d suggest not calling the base classes OnEnd() method as long as it is virtual. If your child class implements an override to the OnEnd() method, then that child class’s implementation will be skipped because you are explicitly calling the base class’s OnEnd. This is a subtle issue that may slip by unnoticed and cause angst later on.
So, unless you have a compelling reason to call the base class explicity, remove “base.” from the “base.OnEnd()” line and just call “OnEnd()” directly. Remember, children have all of the public, protected and internal methods that their parents do.