GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How can they instanciate this Interface like this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can they instanciate this Interface like this?" Watch "How can they instanciate this Interface like this?" New topic
Author

How can they instanciate this Interface like this?

Carl Axel
Greenhorn

Joined: Apr 04, 2012
Posts: 4
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Apr 04, 2012
Posts: 4
Hi Tim,
but how can that be without implementing all the interface methods?

Kind Regards,
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

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.


Do not wait to strike till the iron is hot; but make it hot by striking....
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Apr 04, 2012
Posts: 4
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Carl Axel
Greenhorn

Joined: Apr 04, 2012
Posts: 4
Thanks Steve, I think I get it now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can they instanciate this Interface like this?