About Question enthuware.ocajp.i.v7.2.1219 :
Moderator: admin
-
- Posts: 10
- Joined: Tue Feb 12, 2013 9:12 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1219 :
Got me on this one I had not seen before an interface casting for a method call of an object instance like this before and wondered if it would compile. I am pretty sure this would not only compile but run the same without the interface cast even though both interfaces have it beings there really is only one implementation of the body. My question is, would this always be true. The only exception I can think of possibly is if you had a variable with a class type that doesn't implement a given interface but it is pointing to a derived class instance that does and you want to call a method that is unique to said interface -- is this correct?
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Not completely sure what you mean. Can you put it in code so that it is more clear?
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Tue Feb 12, 2013 9:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Sorry, not sure I should have asked such an open ended question. I was sure the cast to I1 wasn't necessary to call the m1() method and because I have not seen such a thing before I was trying to think of a case where it would be required. I could only think of one possible case and wondered if I was missing something. The following code illustrates what I am talking about:
Code: Select all
interface Printable {void see();}
interface Viewable {void see();}
class Ink{}
class ColorInk extends Ink implements Printable, Viewable {
public void see() {}
}
class InterfacePlay {
public static void main (String... args) {
ColorInk color = new ColorInk();
//
// Even though a method with the same signature see() exists
// in the interface Printable and Viewable that ColorInk
// implements, it doesn't matter because there is only one
// implemented method; therefore, we do not need to cast a
// specific Interface.
//
color.see();
//
// It is also okay to cast specifically to either interface
// but unnecessary.
//
((Viewable) color).see();
((Printable) color).see();
//
// I was trying to understand why you would ever need to
// cast an object to a given interface in order to call
// a method required by said interface. The only case I
// could think of was something like the following, which
// interestingly works and solidified my understanding of
// why instanceof allows comparing to an interface of an
// object whose class doesn't implement the interface but
// is also not final.
//
Ink ink = new ColorInk();
((Viewable) ink).see();
((Printable) ink).see();
}
}
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Yes, you are right. That is a good example. Since an interface can be implemented by any class, the compiler lets you do instanceof between any object and any interface. The same is not true for subclass relationships. If the compiler figures out that an object of the given class can never be an instance of another class, it will not compile. For example: Say you have three classes B extends A and C extends A. the following will not compiler
B b = new B();
if(b instanceof C){ } //this will not compile because a B can never be a C.
B b = new B();
if(b instanceof C){ } //this will not compile because a B can never be a C.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
((Viewable) color).see();
((Printable) color).see();
I understand at runtime the type of Object color is pointing to is ColorInk, yes?
How was it possible that method see() of class ColorInk got called after casting color to Viewable??
Thanks.
((Printable) color).see();
I understand at runtime the type of Object color is pointing to is ColorInk, yes?
How was it possible that method see() of class ColorInk got called after casting color to Viewable??
Thanks.
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
It is possible because there is only one implementation of the see() method. The compiler is not confused between two methods because interfaces do not contain any implementation.
HTH.
Paul.
HTH.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
good enough, i don't want to get into mechanics of this as it would lead to obsessionadmin wrote:It is possible because there is only one implementation of the see() method. The compiler is not confused between two methods because interfaces do not contain any implementation.
HTH.
Paul.
thanks Paul.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Paul, Why wasn't this flagged in compile time?admin wrote:It is possible because there is only one implementation of the see() method. The compiler is not confused between two methods because interfaces do not contain any implementation.
HTH.
Paul.
printable = (Printable)blackInk;
Code: Select all
class Ink{}
interface Printable {}
class ColorInk extends Ink implements Printable {}
class BlackInk extends Ink{}
class TwistInTaleCasting {
public static void main(String args[]) {
Printable printable = null;
BlackInk blackInk = new BlackInk();
printable = (Printable)blackInk; // why this one wasn't flagged at compile time? BlackInk doesn't implement Interface Printable?
}
}
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Because blackInk is just a reference. At runtime it is possible for blackInk to point to a class that extends BlackInk and also implements Printable. So the compiler leaves that for the runtime.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Thank you, please check below.admin wrote:Because blackInk is just a reference. At runtime it is possible for blackInk to point to a class that extends BlackInk and also implements Printable. So the compiler leaves that for the runtime.
why this was flagged then?
based on what you told me it's also possible the ColorInk extends a class that extends BlackInk, yes?
Code: Select all
import java.util.ArrayList;
public class Invalid {
public static void main(String args[]) {
ArrayList<ColorInk> inks = new ArrayList<ColorInk>();
inks.add(new ColorInk());
Ink ink = (BlackInk)inks.get(0); //this got flagged at compile time.
}
}
class Ink{}
class ColorInk extends Ink{}
class BlackInk extends Ink{}
Online
-Paul.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Can you show me how that is possible? In other words, given the code you already have, show me a class that is a ColorInk and is also a BlackInk.based on what you told me it's also possible the ColorInk extends a class that extends BlackInk, yes?
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
right.... i didn't have my while asking the question but thanks for making me think about it.admin wrote:Can you show me how that is possible? In other words, given the code you already have, show me a class that is a ColorInk and is also a BlackInk.based on what you told me it's also possible the ColorInk extends a class that extends BlackInk, yes?
-Paul.
-
- Posts: 2
- Joined: Thu Aug 08, 2013 6:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Admin, I am sorry, the original question does not have " public void m2() { System.out.println("Hello2"); } " Previously, I posted incorrectly.
So...
Question 63 of Test 4 says:
What will be the output of compiling and running the following program:
The program failed to compile and said: "TestClass.java:1: error: TestClass is not abstract and does not override abstract method m2() in I2 ..." As I understand, the compiler is complaining that TestClass says that it is implementing I2, but it is not implementing I2 because there is no void m2() method in TestClass.
So...
Question 63 of Test 4 says:
What will be the output of compiling and running the following program:
Code: Select all
class TestClass implements I1, I2{
public void m1() { System.out.println("Hello"); }
public static void main(String[] args) {
TestClass tc = new TestClass();
( (I1) tc).m1();
}
}
interface I1{
int VALUE=1;
void m1();
}
interface I2{
int VALUE=2;
void m2();
}
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
why casting tc to I1 ( (I1) tc).m1(); does not call m1 from I1 interface, m1 has been called from TestClass and if I1 was a super class (concrete) it does?
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Interfaces do not have any implementation for methods. So you statement "why it does not call m1 from I1 interface" doesn't make sense. Look at it another way, how do you know it does not call m1 from I1?fasty23 wrote:why casting tc to I1 ( (I1) tc).m1(); does not call m1 from I1 interface, m1 has been called from TestClass and if I1 was a super class (concrete) it does?
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
I expected compiler return error as the m1 in I1 doesn't have body because of casting tc to I1 ( (I1) tc).m1(), but instead realized this situation and use m1 in TestClass.admin wrote:Interfaces do not have any implementation for methods. So you statement "why it does not call m1 from I1 interface" doesn't make sense. Look at it another way, how do you know it does not call m1 from I1?fasty23 wrote:why casting tc to I1 ( (I1) tc).m1(); does not call m1 from I1 interface, m1 has been called from TestClass and if I1 was a super class (concrete) it does?
This question helped me a lot to understanding another compiler behavior.
Thanks for great book.
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
When you cast some object to another type, you don't change the object.you are just telling the compiler to treat the object like that type.
Another thing is that references point to objects and not to classes or interfaces, so methods are called on these objects not on a class or an interface.
Another thing is that references point to objects and not to classes or interfaces, so methods are called on these objects not on a class or an interface.
If you like our products and services, please help us by posting your review here.
-
- Posts: 42
- Joined: Mon Jan 20, 2014 2:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
I don't know what about you guys but when I try to compile and run the code it just throws a
ClassCastException and never compiles nor print anything....
The compiler just says that TestClass cannot be cast to I1.
ClassCastException and never compiles nor print anything....
The compiler just says that TestClass cannot be cast to I1.
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
If you are getting CCE, that means you are running it. Compilation is done. Compiler doesn't throw ClassCastException! JVM does.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Apr 28, 2014 9:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Hi admin,
I will highly appreciate and thankful if you can refer me to some internet link where interfaces and casting topics are explained in details.
I have read them already in Mala Gupta book but still there are some questions like in this discussion about which I cannot make crystal clear opinion.
I will highly appreciate and thankful if you can refer me to some internet link where interfaces and casting topics are explained in details.
I have read them already in Mala Gupta book but still there are some questions like in this discussion about which I cannot make crystal clear opinion.
Online
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1219 :
Hi Umair,
You may try reading "Thinking in Java" by Bruce Eckel. This topic is well covered even in the older edition of the book which is available for free.
HTH,
Paul.
You may try reading "Thinking in Java" by Bruce Eckel. This topic is well covered even in the older edition of the book which is available for free.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 20 guests