-2

I have been thinking for a really long time about "implementation hiding in Java". Well, I know it is impossible (at least in theory), but there are some libraries (for example for ZIP files) that need to hide some code (because if anyone could see the code, they will be able to open fe. zip files without knowing the password).

I know that there is a way do make web services, but the application that is using some libraries might be offline app, so web services can't be a point too for everything.

Could you mind helping me to understand it well?

PoLeq
  • 15
  • 2
  • 1
    Ate you looking for [Bytecode obfuscation](https://owasp.org/www-community/controls/Bytecode_obfuscation#:~:text=Bytecode%20Obfuscation%20is%20the%20process,enough%20skill%2C%20time%20and%20effort.)? – Matteo NNZ Jul 08 '22 at 15:45
  • 2
    see also https://stackoverflow.com/questions/14492422/hiding-the-source-code-in-jar-files – John Henckel Jul 08 '22 at 15:47
  • Some of the strongest encryption algorithms are, in fact, Open Source. – k314159 Jul 08 '22 at 16:14
  • 1
    And it is not possible to open a zip file without knowing the password, even if you have the full source code of the zip program. That's because the password is used to encrypt the contents. – k314159 Jul 08 '22 at 16:15
  • 1
    Does this answer your question? [Hiding the source code in .jar files](https://stackoverflow.com/questions/14492422/hiding-the-source-code-in-jar-files) – robni Jul 08 '22 at 21:25

1 Answers1

0

In Java, we often use interfaces to abstract the implementation details and what kind of things you can do with that.

This makes things drop in and easier to inject mocks because the mock just has to provide that interface.

One example is java.util.Map, that has implementations like java.util.HashMap. One could wrap another object to implement the Map interface and pass it to the component that needs a Map.

This makes things much less uncoupled, but personalized interfaces require some extra planning. You could stick with simpler interfaces (Sum, Sub, Mul, and Div instead of a whole calculator) because classes can implement more than one interface but can't extend more than one class.

  • Interfaces they can't be initialized, they need an object of a class that implements that intefcace. So how I can give to user only that interface without that "main" class? It is even possible? And if is could you give me any example, please? – PoLeq Jul 08 '22 at 15:51
  • 2
    What you say is true but is not what's been asked in the question though – Matteo NNZ Jul 08 '22 at 15:52
  • Something like abstract classes? You can defer the implementation of some method to the inherited. That class can't be instantiated as is, to instantiate you must extend the class and implement the missing methods. – Lucas Eduardo Jul 09 '22 at 18:52