0

Today when I read the programs of hibernate, I find out that the interface Session is implemented by 4 implementations, so how can I know which implementation is used when I use the interface Session? Thanks for your time!

Irshu
  • 8,248
  • 8
  • 53
  • 65
  • 4
    You got to test is by `instanceof` I think. But that defeats the purpose of using `interface` in the first place – Ian May 20 '16 at 08:43
  • 1
    In best case you do not need to know. That's the point in using them. When debugging, you can also use stacktrace-information. – Fildor May 20 '16 at 08:46
  • 1
    Why does that matter? The idea of interfaces is to decouple from the implementation. If your design requires you to know about implementation details ... that might indicate a broken design ... You see: you can always do "instanceof" calls; and then make down casts; but if there is a need for downcasts; then, as said: probably your design is broken. – GhostCat May 20 '16 at 08:47
  • 1
    Why do want to know that? That defeats the whole purpose of using interfaces, and, abstraction. – Aakash May 20 '16 at 08:49
  • Thank you. But if it defeats the whole purpose of using interface, why there are more than one implementation?@Aakash – Jiaqi Zheng May 20 '16 at 08:58
  • 1
    @JiaqiZheng look at what the `List` is. The `List` is the base interface for `ArrayList`, `LinkedList` and many more. Each providing a different behavior on how the `List` does work, but each of them basicly just represnts a `List`. Additionally if there should only be one implementation of the interface there would´nt be necessary. – SomeJavaGuy May 20 '16 at 09:04
  • The idea of an interface is that you abstract "the interface" from "the implementation"; allowing you to exchange implementations for example. You want to check out http://stackoverflow.com/questions/8531292/why-to-use-interfaces-multiple-inheritance-vs-interfaces-benefits-of-interface for why we need interfaces in the first place. – GhostCat May 20 '16 at 09:05
  • 1
    Thank you for your answer sincerely! It's really helpful. @Jägermeister – Jiaqi Zheng May 20 '16 at 09:08
  • Thank you for your time, it's a good example!@KevinEsche – Jiaqi Zheng May 20 '16 at 09:09
  • You are welcome. Minor hint on the way out: try to do some prior research the next time. Especially when you are a beginner: it is very likely, that any question that comes up in your mind ... has been asked here by many other beginners (sometimes countless time) before. So: trying to find existing questions is an important task before writing up new questions here. – GhostCat May 20 '16 at 09:14
  • OK, I see. You are very kind.@Jägermeister – Jiaqi Zheng May 20 '16 at 09:29

0 Answers0