Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce private method since interface methods are only public?

Tags:

c#

.net

Interface can be used to force methods implementations but they need to be public.

What if I want to enforce private methods then ?

Update: It's not about preventing to call, it's about ensuring that the private method has been IMPLEMENTED.

So I don't want to use interface per se. I want to impose some style of coding to a team.

like image 883
user310291 Avatar asked Apr 23 '11 16:04

user310291


People also ask

Can we implement private method in an interface?

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.

Can we override private method in interface?

No, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.

Can we have private and protected methods in interface?

In general, the protected members can be accessed in the same class or, the class inheriting it. But, we do not inherit an interface we will implement it. Therefore, the members of an interface cannot be protected.

Can an interface have private methods Java?

As of Java 9, methods in an interface can be private. A private method can be static or an instance method, but it cannot be a default method since that can be overridden.


2 Answers

Interfaces are always by definition public. The only way to enforce the implementation of a protected (private to the outside but accessible to derived classes) method is by subclassing an abstract class that defines that method:

public abstract class A
{
    protected abstract void Foo();
}

public class B : A
{
    protected override void Foo() { }
}

The above will break if you are trying to change Foo's access modifier in B, or forget to provide an implementation for Foo() in B.

Edit:

I believe achieving something close to what you want is possible using a nested class that is only visible to the outside class: This would allow using the private fields of the outer class and at the same time enforcing the implementation of an interface while technically the class is not visible from anywhere else:

public interface IFooWorker
{
    void DoWork();
    int CalculateSomething();
}

public class Foo
{
    private FooWorker _worker;
    private int _currentValue;
    private string _workStatement;


    public Foo()
    {
        _worker = new FooWorker(this);
    }

    private class FooWorker : IFooWorker
    {
        private Foo _outer;
        public FooWorker(Foo foo)
        {
            _outer = foo;
        }

        public void DoWork()
        {
            _outer._currentValue = CalculateSomething();
            _outer._workStatement = "I did work";
        }

        public int CalculateSomething()
        {
            return 42;
        }
    }

}
like image 155
BrokenGlass Avatar answered Oct 13 '22 11:10

BrokenGlass


why would you be dictating private methods through an interface? the individual classes should be black boxes, the interface only defines the public access to that class and should not be concerned with the private internals of that class, only that the public methods work in an intelligent way. So, I guess what I'm saying is don't even think about it, because it's a ludicrous concept that completely goes against what this feature of the language is trying to accomplish.

like image 34
stephenbayer Avatar answered Oct 13 '22 12:10

stephenbayer