2

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
    }
 }
ygongdev
  • 264
  • 3
  • 19

3 Answers3

5

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;
    }
 }
toadflakz
  • 7,764
  • 1
  • 27
  • 40
  • It seems that the OP also needs to make the class / method abstract, you might want to add that information as well. – Luaan Jul 08 '16 at 14:56
  • Not sure that he does as he didn't specifically say that B must not be instantiable. ;) However, he does need to clarify about `IsValidRow()` as it does read as if he is saying it must be implemented in a child class rather than optionally implemented. – toadflakz Jul 08 '16 at 14:58
  • Mmm. for class A, what if I don't intend to implement anything in Transform(), I will only implement Transform() in class B? I mean I could see myself returning some garbage in the base class virtual method and override it in all the derived – ygongdev Jul 08 '16 at 14:58
  • @ygongdev And if you cannot use an abstract base class you can always make it virtual and throw a` NotImplementedException()` – Bob Vale Jul 08 '16 at 15:08
  • Do you need to call isValidRow from your base class or just from child classes ? – Nadeem Khoury Jul 08 '16 at 15:09
  • @NadeemKhoury, since IsFileValid calls on IsValidRow and IsFileValid is in the base class, it would be called from just from the base class, but the implementation for IsValidRow lies within each individual child class – ygongdev Jul 08 '16 at 15:11
0

You can mark the method IsValidRow()as abstract:

namespace test
{
    interface A
    {
        bool IsFileValue();
        bool IsValidRow();
    }

    abstract class B : A
    {
        public bool IsFileValue()
        {
            return IsValidRow();
        }

        public abstract bool IsValidRow();
    }

    class C : B
    {
        public override bool IsValidRow()
        {
            return true;
        }
    }
}
Udo
  • 449
  • 3
  • 13
0

I have a solution for you is to use Interface Segregation principle. what does that mean is to declare two interfaces. The first one have one method which is IsFileValid() and the second interface implement the first interface and has another method which is IsFileValid() like this

  Interface A { void IsFileValid(); }
  Interface B : A { void IsValidRow();}

and the base class implement interface A and all the derived classes implement the interface B like this.

public class Base : A
{
      void IsFileValid(){ // immplementaion details}
}

public class Child : Base, B
{
      void IsValidRow(){// immplementaion details}    
}

Happy Coding

Nadeem Khoury
  • 886
  • 6
  • 18
  • That won't solve the OP's problem as the base class needs to call `IsValidRow` on itself but have the implementation in the child class. – Bob Vale Jul 08 '16 at 15:06
  • yes you are right, but I think he says that he needs to call IsValidRow only from child classes. – Nadeem Khoury Jul 08 '16 at 15:08
  • No OP states that `IsValidRow` is called from `IsFileValid` and that base class implements `IsFileValid` and child class implements `IsValidRow` – Bob Vale Jul 08 '16 at 15:10
  • ahh okay, so you are totally right. thank you so much for your pointing out. – Nadeem Khoury Jul 08 '16 at 15:11