About Question enthuware.ocajp.i.v7.2.1219 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
satar
Posts: 10
Joined: Tue Feb 12, 2013 9:12 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1219 :

Post by satar »

Got me on this one :shock: 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.

satar
Posts: 10
Joined: Tue Feb 12, 2013 9:12 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by satar »

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();
		
	}
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by baptize »

((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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by baptize »

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.
good enough, i don't want to get into mechanics of this as it would lead to obsession :mrgreen:

thanks Paul.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by baptize »

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.
Paul, Why wasn't this flagged in compile time?
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?
     }
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by baptize »

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.
Thank you, please check below.

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{}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

based on what you told me it's also possible the ColorInk extends a class that extends BlackInk, yes?
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.
-Paul.
If you like our products and services, please help us by posting your review here.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by baptize »

admin wrote:
based on what you told me it's also possible the ColorInk extends a class that extends BlackInk, yes?
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.
-Paul.
right.... i didn't have my :ugeek: while asking the question but thanks for making me think about it.

dserge01
Posts: 2
Joined: Thu Aug 08, 2013 6:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by dserge01 »

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:

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();
}
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.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by fasty23 »

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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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?
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?
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by fasty23 »

admin wrote:
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?
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?
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.

This question helped me a lot to understanding another compiler behavior.
Thanks for great book.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by Nisim123 »

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.
:roll:

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.

UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by UmairAhmed »

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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1219 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 38 guests