Hiding methods from other classes when inheriting from a class and Interface or Abstract class

897 views Asked by At

To improve further implementation and to offer some guidelines and keep everything as universal as possible within the project I've created an Interface with a few methods which should be used. However only one method of this class should be visible to the user calling that class so I'd like them to be of the protected variety. E.g

public class ClassThree
{
    public ClassThree()
    {
        var classOne = new ClassOne();
        class1.MethodOne();
    }
}

This despite ClassOne having 4 methods, 3 methods are used only within the one public class, hence private or protected. These 3 methods are required to make the 4th method work however (in an ideal world other developers would follow the same principle). So I don't want to see the following pop up on intellisense:

class1.MethodTwo();
class1.MethodThree();
class1.MethodFour();

I know one can implicitly call methods from an Interface e.g

IInterface.MethodTwo<Type,Type>(string name)
{
  //Do stuff here
}

However I would like to prevent all the casting when calling said methods in ClassOne itself since this is just a thorn in my eye. I like my code clean and this isn't clean at all to me. I've thought of an abstract class however my class is inheriting from another class. With the interface I could just do

public ClassOne : ClassTwo, IInterface<Type1,Type2>

When I do that with an Abstract class however Visual Studio says an interface is expected. Any insights are most welcome and appreciated as I would like to up my code by making my life and that of fellow developers, who have to use my code, easier.

Thanks in advance!

Edit: The scenario is there can be several classes like ClassOne which essentially do the same however they use different types since the objects they have to return hold different values. However the buildup to these objects are more or less the same e.g:

  1. Collect all API data
  2. Retrieve the list to be exported to the API and call #3 or #4 depending on the type.
  3. Export Type 1 to the API
  4. Export Type 2 to the API

The idea is always the same but naturally different API's will require different variables. But to ensure all steps are followed as before I'd like to implement an Interface or something but step 1,2 and 3 should be private or protected and only step 2 should be available to the class that consumed it. However if I only put method 2 in the interface I can never be sure that others will implement 1,3 & 4. And that's kind of the goal here :P. This while ClassOne also inherits from another class and not just the interface.

Edit 2: I know Interfaces only provide public methods which is why I'm looking for alternatives hence this question. I know what is wrong with it I just don't really see how I can get it the way I would like it to be. Thanks for the replies so far!

Edit 3: Interface currently looks like this, I just adjusted variable names for sake of example.

   public interface IExport<in T, in TU>
    {
        void GetRequiredApiData();

        bool MethodOne(List<Type> list);

        bool ExportOne(T one);

        bool ExportTwo(TU two);

        bool ValidateExport();
    }
1

There are 1 answers

2
moarboilerplate On BEST ANSWER

The other three methods should just be called inside your public method, or make your first method private/protected and call all 4 inside one public method.

The whole point of an interface is to hide exactly what is problematic for you--the other three methods that the caller doesn't need to know about.

I suspect you need to have those three methods called at separate, specific times, in a specific order. If that's the case then you have a code smell. Those methods are probably just void subroutines that have side effects and change global state. The code needs to be refactored to not be just a series of subroutines and objects need to be broken out differently.