Results 1 to 6 of 6
  1. #1
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.
    InitialContext envContext = (InitialContext)initContext.lookup("java…
    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.

  3. #3
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default 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 !!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Where is the implementation part when the object is returned from lookup?
    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.

  5. #5
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    I have now understood what you mean when you say :
    Quote Originally Posted by Norm View Post
    The object returned has the methods defined in the Context interface. That's what "implements" means.
    I have a small doubt, when you say :

    Quote Originally Posted by Norm View Post
    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.
    (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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    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:
    Java Code:
    List myList = new ArrayList();
    Unless you need the specific stuff from ArrayList (incredibly unlikely) then stick with List.

    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.
    Java Code:
    public List getMyList() {
        return myList;
    }
    I'd go into the testing advantages of this as well, but that might be info overload.

Similar Threads

  1. help me about Interface
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 05:15 PM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •