Results 1 to 17 of 17
Thread: Question on interfaces
- 01-22-2012, 10:44 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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.
Does this make sense?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.
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.
-
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:
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.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(); }Last edited by Fubarable; 01-22-2012 at 11:06 PM.
- 01-22-2012, 11:08 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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.
- 01-22-2012, 11:12 PM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question on interfaces
Regarding your testcode:
I think you should implement interface I to class Foobar, because it says:
But then, 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.
Very confusing..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.
-
Re: Question on interfaces
Precisely.
I think the second quote doesn't agree with the first quote. I think that the second quote is wrong.But then, what does this mean:
Very confusing..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.
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.
- 01-22-2012, 11:16 PM #6
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question on interfaces
Thanks.
-
- 01-22-2012, 11:17 PM #8
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question on interfaces
I will e - mail SAM , see if he has anything to say about this.
- 01-23-2012, 11:05 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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...:)
- 01-23-2012, 12:57 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
Re: Question on interfaces
What about this?
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:interface I { void m(); } abstract class P implements I { } class C extends P { public void m() { ... } }
kind regards,Java Code:P p= new C(); I i= (I)p; i.m();
JosLast edited by JosAH; 01-23-2012 at 04:17 PM. Reason: made the implementation of m() public ...
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-23-2012, 01:01 PM #11
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question on interfaces
The problem is with :
Does "actually implement" means, Class C should be implementing I ?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.
If implementing an interface through extending a class that implements the interface is meant here, then I guess you are right.
- 01-23-2012, 01:07 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
Re: Question on interfaces
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-23-2012, 02:04 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-23-2012, 02:14 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
- 01-23-2012, 02:16 PM #15
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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();
- 01-23-2012, 02:31 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
Re: Question on interfaces
But it even works if the fist line were:
... because class P implements interface I.Java Code:P p= new C();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-23-2012, 02:33 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Interfaces
By blobbyx22 in forum New To JavaReplies: 4Last Post: 11-27-2011, 10:35 AM -
Interfaces
By dalu in forum New To JavaReplies: 4Last Post: 04-26-2011, 12:22 PM -
Interfaces
By blug in forum New To JavaReplies: 3Last Post: 03-08-2011, 04:01 AM -
Interfaces
By Kavana Krishnappa in forum New To JavaReplies: 7Last Post: 12-11-2007, 04:28 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks