Results 1 to 2 of 2
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Rep Power

    Default Reference Types - Interfaces?

    In this tutorial, a reference type is given as

    "If you declare a variable using the name of a class for its type, that variable can contain a reference to an object of that class. Such a variable does not contain an actual object, but rather a reference to the class instance, or object, the variable refers to. Because using a class name as a type declares a reference to an object, such types are called reference types."

    OK I got it.

    But then they say, "Java also allows the use of an interface name to specify a reference type."
    What I have understood from this is that a reference to an object of the class which implements the interface can be stored in a reference variable declared with the name of the interface implemented by its class for its type.
    But then I was thinking, why would I even do that? When I can simply create the reference variable with the name of the class for its type, why would I do that with an interface being implemented by its class.
    Last edited by JosAH; 10-12-2013 at 08:25 PM. Reason: removed all font, size, boldace, italics tags; it was a mess ...
    Don't forget to smile :-)

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power

    Default Re: Reference Types - Interfaces?

    Why? Maybe you want to be able to assign objects of different classes that implement the interface to that variable. Or maybe you don't even know what class the object you're referring to actually is. Sometimes factory methods return instances of private classes that implement known interfaces.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 3
    Last Post: 07-24-2013, 05:03 PM
  2. Interfaces
    By blobbyx22 in forum New To Java
    Replies: 4
    Last Post: 11-27-2011, 10:35 AM
  3. Interfaces (not GUI)
    By guilty in forum Advanced Java
    Replies: 1
    Last Post: 11-05-2010, 07:16 PM
  4. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 05:52 PM
  5. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 04:28 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