Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default A question on Interfaces.

    This is from the book SAMS Java in 21 Days:

    Remember that almost everywhere that you can use a class, you can use an interface
    instead. For example, you can declare a variable to be of an interface type:
    Iterator loop = new Iterator()
    When a variable is declared to be of an interface type, it simply means that the object is
    expected to have implemented that interface. In this case, because Iterator contains an
    object of the type Iterator, the assumption is that you can call all three of the interface’s
    methods on that object: hasNext(), next(), and remove().
    Especially this:

    Remember that almost everywhere that you can use a class, you can use an interface
    instead.
    What does that mean ?

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

    Default Re: A question on Interfaces.

    If its is referring to an object's datatype, then there is no difference between a class that extends a class or a class that implements an interface. Both techniques will give the class a datatype.
    When a method requires an object of a specific datatype, like when adding a listener, that datatype can be given to the object in the two ways.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on Interfaces.

    When a method requires an object of a specific datatype, like when adding a listener, that datatype can be given to the object in the two ways.
    So if a method requires a type of Interface f, and if my class MyClass is implementing f, that method can get a reference of type MyClass. Is this all that text is saying ?

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

    Default Re: A question on Interfaces.

    What you say seems correct. I don't know what the author of the text meant.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

Similar Threads

  1. Question on interfaces
    By fatabass in forum New To Java
    Replies: 16
    Last Post: 01-23-2012, 02:33 PM
  2. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 04:58 PM
  3. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 05:52 PM
  4. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 04:28 PM
  5. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 08:11 AM

Posting Permissions

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