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.
Your colleague is traveling. Your private methods are already... private.
– Math
It implies that it is necessary. To hide the private methods of the Main class
– msm.oliveira
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.
– Math
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
– msm.oliveira
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?
– Raimundo Norberto
You’re saying that "Interfaces are meant to hide private methods from main. Which are Object-Oriented Programming rules."
– msm.oliveira
@msm.oliveira Next, if your method
main
is in the class itself, or if your class is internal the class containing themain
, then nothing will stop themain
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).– mgibsonbr
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
– msm.oliveira