• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can they instanciate this Interface like this?

 
Carl Axel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am working with a java API from Lmax used for trading. I am getting things to work but a use of an interface bothers me.
The interface is called Session with many methods however it gets instanciated like this and not implemented as an Interface:



How is this possible?

Best
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see "Session" -whatever that is- getting instantiated anywhere: it gets passed in via the onLoginSuccess method and then stored and used later on.
 
Carl Axel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
but how can that be without implementing all the interface methods?

Kind Regards,
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see Session is being initialized anywhere, its just being used as reference.
Isn't is possible that session is being referenced to some concrete class object which is not pasted in code.
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the question. The class you posted does not claim to implement (or extend) "Session". It merely uses such an object that gets passed to it.
 
Carl Axel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to think about this a little , I am confused. Yes, it is just a reference but the Session class has no implementation anywhere just an Interface.
Thanks anyway !
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl Axwl wrote:I have to think about this a little , I am confused. Yes, it is just a reference but the Session class has no implementation anywhere just an Interface.
Thanks anyway !


Session has no public implementation anywhere. The code you call is designed to be registered with some other framework which calls onLoginSuccess() when appropriate. That external framework is charged with providing a session object. The framework must have some specific implementation of Session somewhere, but the implementation and specifics aren't important to this code. The only thing this code needs to know is the session implements the Session interface and therefore provides the functionality required by this code.

That is the beauty of interfaces. This code says 'give me an Object that behaves like the Session interface says it should' and the caller can provide any Object which implements the interface. It could be one that works with Databases to store data, or XML files, or memory, or synchronizes with multiple systems, etc... But this code doesn't need to know that. And if there is no obvious Session implementation in whatever framework you are working in, then it indicates the framework writers think you don't need to know the implementation details either - the implementation might be protected, private, or internal to some other class.
 
Carl Axel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve, I think I get it now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic