I know the question is confusing, but it is hard to think of a good title for this problem, so I'll describe more here.
I have an interface A with 2 methods, IsValidRow() and IsFileValid()
I have a base class B and a derived class C
The goal here is that I want the base class to implement IsFileValid(), so it can be inherited by all the classes derived off the base class and each derived class to implement IsValidRow().
The problem is that IsFileValid() calls IsValidRow() inside. If I do
B:A
A requires IsValidRow() to be implemented in B.
Currently my derived class C inherits from the base class and the interface atm.
I don't mind restructuring everything as long as the requirements for the 2 methods are fulfilled (One will be implemented once in a base class or something and inherit across, while the other one will be implemented in each derived class)
interface IFileValidator
{
Pair<bool, string> IsValidRow(List<string> row);
bool IsFileValid(string file);
}
class FileValidator : IFileValidator
{
public bool IsFileValid(string file)
{
// calls IsValidRow()
IsValidRow();
}
}
class FacilitiesCalendarValidator : FileValidator, IFileValidator
{
public Pair<bool, string> IsValidRow(List<string> row)
{
//stuff
}
}
It is possible for both cases.
For optionally overridable methods, declare the base class methods as virtual and override in the child class.
For methods that must have implementation provided by a child class, mark the class and the method as abstract.
You don't have to override a virtual method in a child class so the implementation is inherited unless you explicitly decide to override.
For example:
interface IInterface
{
int Transform(int a, int b);
int AnotherTransform(int a, int b);
}
abstract class A : IInterface
{
public virtual int Transform(int a, int b)
{
return a + b;
}
public abstract int AnotherTransform(int a, int b);
}
class B : A
{
public override int Transform(int a, int b)
{
return a - b;
}
public override int AnotherTransform(int a, int b)
{
return a * b;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With