Results 1 to 6 of 6
Thread: interface and objects
- 07-31-2010, 12:16 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
interface and objects
I'm trying to understand, how the interface "Context" (based on java doc API ) in this case is used to create an object of type Context. From what I had learnt, objects can be created only from classes not interface. Can some one share some light on this concept.
//This is how one creates a
import javax.naming.Context;
import javax.naming.InitialContext;
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/…
DataSource ds = (DataSource)envContext.lookup("jdbc/myDB…
Can I write.
--------------------------------------…
InitialContext initContext =new InitialContext();
InitialContext envContext = (InitialContext)initContext.lookup("java…
DataSource ds = (DataSource)envContext.lookup("jdbc/myDB…
If yes, that's great. But would like to know why in all google realted sites I see Context initContext = new InitContext(); Is there some importance that I am missing. Thank you in advance for your responses.
- 07-31-2010, 12:32 AM #2
When a class implements an interface that gives the class an addition type. Java is a strongly typed language.
An object of that type can then be used when it is required for example in a method call.
A commonly used type is an ActionListener which is the required type for the addActionListener() method.
Can I write.
Probably not. The Object returned from lookup() could implement the Context interface but NOT be an InitialContext object. An object of type Context guarantees some methods. An object of type InitialContext has those methods plus possiblity some extra ones.InitialContext envContext = (InitialContext)initContext.lookup("java…
- 07-31-2010, 01:35 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
implementation part when the object is returned
Thanks for the response.
I got a mojor postion of what you are saying but still didn't understand thte following
" The Object returned from lookup() could implement the Context interface but NOT be an InitialContext object"
Where is the implementation part when the object is returned from lookup?
Thanks !!
- 07-31-2010, 02:09 AM #4
The object returned has the methods defined in the Context interface. That's what "implements" means. Any code working with that object can call any of the methods defined in the Context interface. Implements means that they will be there.Where is the implementation part when the object is returned from lookup?
- 08-02-2010, 08:18 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
I have now understood what you mean when you say :
I have a small doubt, when you say :
(For creating an object)Does it always mean the class that implements an interface you follow this standard:
Interface_name variable = new Class_that_implements_interface.
If no, can you let me know why?. Im just trying to get a clear undertanding. Ur responses have been very helpful. Thank you.
- 08-02-2010, 11:50 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Generally, the best practice in Java is to code to the interface. That is to use the least specific class to define an object as you can...eg:
Unless you need the specific stuff from ArrayList (incredibly unlikely) then stick with List.Java Code:List myList = new ArrayList();
The advantage of this is that you can replace ArrayList with LinkedList should you find a perfromance advantage (say) in doing so, and you only have to do it in one place. This becomes important in method definitions.
I'd go into the testing advantages of this as well, but that might be info overload.Java Code:public List getMyList() { return myList; }
Similar Threads
-
help me about Interface
By manhtungtnk28@gmail.com in forum New To JavaReplies: 3Last Post: 11-23-2009, 05:15 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks