I’m refactoring my code, so I need make decision about interface or abstract class.
I have base class Player and classes that inherit base class which are called VideoPlayer, MusicPlayer and so on. Base class have abstract method with no implementation (Play).
So, What is preferable way? Put Play in interface or leave it in abstract class. Play in MusicPlayer is not same like Player in VideoPlayer. I do that in C#.
class Player
{
abstract void Play();
}
class VideoPlayer : Player
{
void Play()
{
//Some code.
}
}
class MusicPlayer : Player
{
void Play()
{
//Some code.
}
}
One common thing is to do both
a) provide the interface. And use the interface when you consume the object (ie invoke the play method).
b) provide a base class that implements the interface for the case where there is common plumbing; common methods etc. This is a helper for implementers to optionally use
IN this way an implementer of IAmAPlayer can simply implement that interface or if their use case matches your base class they can use that.