Interface vs Class - method visibility

Asked

Viewed 369 times

0

I did a job for school and it’s working perfectly.

In the code I have some private methods. I need to create an Interface of this class to hide these private methods or they can be in the same class as the public methods?

Right now I have all the methods (public and private) in the same class, but my group colleague is insisting that he has to create an interface to hide the private methods.

  • 2

    Your colleague is traveling. Your private methods are already... private.

  • It implies that it is necessary. To hide the private methods of the Main class

  • It depends on who you want to hide, if you want to hide from the class itself you need to put them in another class, but not in an interface because they have no implementation. Pole one Minimum, Complete and Verifiable Example so that we can understand what is happening. Moreover, it is not possible to create a private method in an interface.

  • The work works as it should be and I have already made a submission and passed. Now we are just trying to improve it. My colleague insists on an interface for private methods

  • Friend, if the method is private no other class can invoke it (by "normal" means). There is no reason to leave it "more hidden". What argument is he using as justification?

  • You’re saying that "Interfaces are meant to hide private methods from main. Which are Object-Oriented Programming rules."

  • @msm.oliveira Next, if your method main is in the class itself, or if your class is internal the class containing the main, then nothing will stop the main access your private members. Otherwise, you don’t have to do anything. Interfaces serve to decouple the "specification" (contract) from the "implementation", has nothing to do with hiding private members (because as Math said, if they are private, they are private, and point).

  • I have the Main class that will interact with the user, in this case, the console. And I have the Zombies class where the algorithm is and the accounts necessary for the program to run properly. The programme is up and running 100%, my colleague says that the interface should be put because of the private methods

Show 3 more comments

1 answer

5


Interfaces are nothing more than purely abstract classes. That is, classes that do not have defined members (i.e. impose no implementation detail), and all their methods are public and abstract (i.e. they establish a contract that the implementing class must offer to the consumer code). For this reason, a reference to an interface does not have access to any private member of the particular class.

However, this is a condition sufficient to hide private members, but not necessary - it is perfectly possible to obtain encapsulation without user interfaces. Encapsulation by private only does not apply to the class itself (obviously, because the class needs to have access to its own fields!) and - in case one class is internal to the other class (Inner class) - to the class "outside".

Here is an example of what is and is not encapsulated in various situations:

class Main {
    private int podeAcessar;

    interface IInterna { }

    abstract class InternaAbstrata {
        private int podeAcessar2;
    }

    class Interna extends InternaAbstrata implements IInterna {
        private int podeAcessar3;
    }

    public static void main(String[] args) {
        Main a;            // pode acessar os campos privados

        Interna b;         // pode acessar os campos privados
        InternaAbstrata c; // pode acessar os campos privados
        IInterna d;        // NÃO pode acessar os campos privados

        Externa e;         // NÃO pode acessar os campos privados
        ExternaAbstrata f; // NÃO pode acessar os campos privados
        IExterna g;        // NÃO pode acessar os campos privados
    }
}

interface IExterna { }

abstract class ExternaAbstrata {
    private int naoPodeAcessar;
}

class Externa extends ExternaAbstrata implements IExterna {
    private int naoPodeAcessar2;
}

As you can see in the example above, this statement that "... you have to create an Interface to hide private methods" is incorrect, because both in the case of Externa how much of ExternaAbstrata encapsulation occurs even without the use of interfaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.