Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By fatabass

Thread: Question on interfaces

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

    Default Question on interfaces

    What does this mean:

    In addition to casting objects to classes, you also can cast objects to interfaces, but only
    if an objectís class or one of its superclasses actually implements the interface. Casting
    an object to an interface means that you can call one of that interfaceís methods even if
    that objectís class does not actually implement that interface.

    This is from book: SAMS Teach Yourself Java in 26 Days.

    Casting an object to an interface means that you can call one of that interfaceís methods even if
    that objectís class does not actually implement that interface.
    Does this make sense?

    Doesn't a class have to implement all the methods it implements ?

    And it says it has to implement the interface if you are to cast it.

    I am confused.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on interfaces

    I've never seen that done, and even if it compiles, it doesn't "smell" right to me. I think you are right to question this text.

    edit: test code below shows that this compiles but as expected throws a ClassCastException:

    Java Code:
    public class InterfaceTest {
       public static void main(String[] args) {
          Fubar fubar = new Fubar();
          I myI = (I)fubar;
          myI.iMethod();
       }
    }
    
    class Fubar {
       public void iMethod() {
          System.out.println("in Fubar's iMethod");
       }
    }
    
    interface I {
       void iMethod();
    }
    Which proves that your worries are not unfounded, and the book looks to be wrong. I never liked that book and don't recommend it.
    Last edited by Fubarable; 01-22-2012 at 11:06 PM.

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

    Default Re: Question on interfaces

    Can I get book recommendations ?

    I am trying to decide on a book to follow, but it looks more difficult than learning JAVA itself.

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

    Default Re: Question on interfaces

    Regarding your testcode:

    I think you should implement interface I to class Foobar, because it says:

    In addition to casting objects to classes, you also can cast objects to interfaces, but only
    if an object’s class or one of its superclasses actually implements the interface.
    But then, what does this mean:
    Casting an object to an interface means that you can call one of that interface’s methods even if
    that object’s class does not actually implement that interface.
    Very confusing..

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on interfaces

    Quote Originally Posted by fatabass View Post
    Regarding your testcode:

    I think you should implement interface I to class Foobar, because it says:
    Precisely.


    But then, what does this mean:
    Casting an object to an interface means that you can call one of that interface’s methods even if
    that object’s class does not actually implement that interface.
    Very confusing..
    I think the second quote doesn't agree with the first quote. I think that the second quote is wrong.

    Regarding book recommendations, it depends on your method of learning. Some love the head first book, others like Thinking in Java. Eventually you'll want more of a reference book such as the Core Java Vol 1 and 2 books.

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

    Default Re: Question on interfaces

    Thanks.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on interfaces

    Quote Originally Posted by fatabass View Post
    Thanks.
    You're welcome.

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

    Default Re: Question on interfaces

    I will e - mail SAM , see if he has anything to say about this.
    DarrylBurke likes this.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Question on interfaces

    I suppose technically you could cast to an interface that the object being cast does not implement and still call the method in the code.
    It would compile, but would fail at runtime...:)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question on interfaces

    What about this?

    Java Code:
    interface I { void m(); }
    abstract class P implements I { }
    class C extends P { public void m() { ... } }
    Class P doesn't implement method m() although it claims to implement interface I (it can do so because class P is abstract). The following fragment runs without errors:

    Java Code:
    P p= new C(); 
    I i= (I)p;
    
    i.m();
    kind regards,

    Jos
    Last edited by JosAH; 01-23-2012 at 04:17 PM. Reason: made the implementation of m() public ...
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Question on interfaces

    The problem is with :

    Casting an object to an interface means that you can call one of that interface’s methods even if
    that object’s class does not actually implement that interface.
    Does "actually implement" means, Class C should be implementing I ?

    If implementing an interface through extending a class that implements the interface is meant here, then I guess you are right.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question on interfaces

    Quote Originally Posted by fatabass View Post
    The problem is with :



    Does "actually implement" means, Class C should be implementing I ?

    If implementing an interface through extending a class that implements the interface is meant here, then I guess you are right.
    Yep, class P in my example doesn't actually implement method m() as mentioned in interface I; it is class C that extends P, that implements method m() so everything will run fine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Question on interfaces

    Ah, but "that object's class" is not P...it is C.
    So that sentence is really badly worded if that is what they meant.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question on interfaces

    Quote Originally Posted by Tolls View Post
    Ah, but "that object's class" is not P...it is C.
    So that sentence is really badly worded if that is what they meant.
    Yep, I agree, but the example I sketched cowardly slipped through the wordings of that book ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Question on interfaces

    Yeah I agree with Tolls.

    It should work in this situation I guess, according to the book:

    Java Code:
    C p= new C(); 
    I i= (I)p;
     
    i.m();

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question on interfaces

    But it even works if the fist line were:

    Java Code:
    P p= new C();
    ... because class P implements interface I.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Question on interfaces

    Quote Originally Posted by JosAH View Post
    Yep, I agree, but the example I sketched cowardly slipped through the wordings of that book ;-)

    kind regards,

    Jos
    So does mine.
    It just crashes at runtime...:D
    The wording never says anything about actually running!

Similar Threads

  1. Interfaces
    By blobbyx22 in forum New To Java
    Replies: 4
    Last Post: 11-27-2011, 10:35 AM
  2. Interfaces
    By dalu in forum New To Java
    Replies: 4
    Last Post: 04-26-2011, 12:22 PM
  3. Interfaces
    By blug in forum New To Java
    Replies: 3
    Last Post: 03-08-2011, 04:01 AM
  4. 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
  •