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

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

Moderator: admin

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

Post by Jix »

How come option (a = (I) b;) is wrong?
The explanation states that: An I is not an A. Therefore, it will not compile.

but declaration of class A was like this:
class A implements I

so clearly I is an A

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

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

Post by admin »

Jix wrote:How come option (a = (I) b;) is wrong?
The explanation states that: An I is not an A. Therefore, it will not compile.

but declaration of class A was like this:
class A implements I

so clearly I is an A
No, A is an I. I is not an A. You seem to be confused. Think like this:
Car implements Movable : Is Car a Movable or is Movable a Car?

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

Post by Jix »

Thanks for answering but I'm still confused, if (a = (I) b;) is wrong cuz I is not an A then why (a = (B)(I)b;) is fine? b is already an object of class B and (a=b;) is fine, so the cast of ( (B)b ) isn't necessary... so what will casting ( (B)(I)b ) do to make it legit? isn't it the same as ( (I)b )?

Thank you fro your time

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

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

Post by admin »

An I is not an A or a B but since it is an interface, a reference that is pointing to an I, may actually be pointing to any class that implements I. So if you put an explicit cast as in (B)(I) b; compiler is satisfied. Of course, it must hold true at run time also other wise it will throw a ClassCastException.
If you like our products and services, please help us by posting your review here.

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

Post by Jix »

ok i get it, thanks

evgen

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

Post by evgen »

a = (B)(I)b; What happens in this statement and where i can read about it. Thanks. From Russia with love.))

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

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

Post by admin »

A multiple cast is not much different from a single one. You should break a multiple into two single casts as shown below:

b = (B)(I) a;

is same as:

I temp = (I) a;
b = (B) temp;

That's it. There is nothing more to it.
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.1328 :

Post by baptize »

a = (B) (I) b;
I'm confused....
class (B) extends (A) which implements (I). So class (B) contains (A) and (I).

so...

I temp = (I) B; // B is I
a = (B) temp; // but object temp is not B?

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

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

Post by admin »

baptize wrote: So class (B) contains (A) and (I).
No, B does not contain A or I. B is A and A is I. So B is A and I
I temp = (I) B; // B is I
a = (B) temp; // but object temp is not B?
I am not sure what you mean. If a Human Being is an Animal, that does not mean every Animal is Human Being!
So B is I doesn't mean I is B.

You have a very fundamental doubt. I would suggest you to go through a book to understand these basics before attempting mock exams.

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

Post by baptize »

admin wrote:
baptize wrote: So class (B) contains (A) and (I).
No, B does not contain A or I. B is A and A is I. So B is A and I
I temp = (I) B; // B is I
a = (B) temp; // but object temp is not B?
I am not sure what you mean. But if a Human Being is an Animal, that does not mean every Animal is Human Being!
So B is I doesn't mean I is B.

You have a very fundamental doubt. I would suggest you to go through a book to understand these basics before attempting mock exams.

HTH,
Paul.
you had to say it Paul....... :evil:

I got it.

A a = (B)(I) b;
can be broken down to..
I i = b; // i don't even need the (I) here because B is I (upward cast).
A a = (B) i; // here (B) is needed to pass the compiler time error. At runtime i points to an Object of type B.

how did i do :ugeek: !!

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

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

Post by admin »

Try it out :)
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.1328 :

Post by baptize »

admin wrote:Try it out :)
already did :shock:

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

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

Post by admin »

So what did you find out?
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.1328 :

Post by baptize »

this:
A a = (B)(I) b;
can be broken down to..
I i = b; // i don't even need the (I) here because B is I (upward cast).
A a = (B) i; // here (B) is needed to pass the compiler time error. At runtime i points to an Object of type B.

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

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

Post by admin »

Very good :)
If you like our products and services, please help us by posting your review here.

Codeguru
Posts: 3
Joined: Fri Mar 29, 2013 2:53 pm
Contact:

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

Post by Codeguru »

So, I i = (C) a fails because you can't cast an object to a class that I is not, you can only reference to a class that is an I, like that has been said I i = b, and then using polymorphism do a A a = (C) b, since C is a B is an A is an I? Or does the cast on the b here need to be the actual class that it is in runtime, which is B?

If it looks like I have no idea how polymorphism works, it's because I don't. :) God help me when I attain a Java developer position...

bptoth
Posts: 33
Joined: Mon May 06, 2013 9:41 am
Contact:

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

Post by bptoth »

Just a small question about

Code: Select all

I i = (C) a;
The explanation is as follows: It will compile because a C is-a A, which is-a I, and a reference of class A can point to an object of class C. But it will fail at runtime because a does not point to an object of class C.

Didn't the bold part possibly wanted to state: a reference of interface I can point to an object of class C?
Just because this is what actually seems to happen here
It is definitely true in its current form as well, but does not seem to exactly cover the scenario, and also seems to be redundant, given that "C is-a A" has been already stated in the first half of the sentence

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

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

Post by admin »

No, 'a' is declared as a reference of class A. Since the given code is "(C) a" the explanation says that a reference of class A can point to an object of class C to explain why 'a' can be cast to C.

The fact "A implements I" is the basis for a valid assignment of a to i.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

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

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

Post by admin »

BTW, the exact rules for casting are given by JLS here: http://docs.oracle.com/javase/specs/jls ... #jls-5.5.1
One should try these out by going through the specification and writing sample programs to improve understanding.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

Hello,

I thought I got this icasting concept but am afraid I don't follow...Keep answering these questions wrong!
We have this (slight variation of the question's class hierarchy but esentially the same):

Code: Select all

package LearnJava;

public class LearnJava {
	public static void main(String args[]) {
		A a = new A();
		B b = new B();
		a = (B)a;
	}
}
interface I{
}
class A implements I{
	void a(){
		System.out.println("Method a() from A");
	}	
}

class B extends A {
	void b(){
		System.out.println("Method b() from B");
	}
}
If C is-a B is-a A is I why can't I cast var a to b by:

a = (B)a;?

The compiler says
"Exception in thread "main" java.lang.ClassCastException: LearnJava.A cannot be cast to LearnJava.B"
Thanks,

Marc
Last edited by admin on Mon Nov 25, 2013 1:44 pm, edited 2 times in total.
Reason: Please use code tags

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

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

Post by admin »

I don't see any C in your code.
The code you've quoted above will fail at run time because a is pointing to an object of class A, and you are trying to cast it as B. So why do you think it should work? B is A but A is not B.

-Paul.
If you like our products and services, please help us by posting your review here.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

Here is code like in question. Added functions in class for clarity:

package LearnJava;

interface I{
}
class A implements I{
void a1(){
System.out.println("A.m1()");
}
}

class B extends A {
void b1(){
System.out.println("B.m1()");
}
}

class C extends B{
void c1(){
System.out.println("C.m1()");
}
}
//And the following declarations:
class LearnJava{
public static void main(String[] args) {
A a = new A();
B b = new B();
a = (B)(I)b;
a.b1();
}
}

Line a = (B)(I)b; compiles ok and gives no RE as the correct answer says. I don't understand what's happening here. Is it a cast from a to b? If so, why does a.b1(); give an compiler error "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: LearnJava.A.b1"?

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

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

Post by admin »

It will be a bit difficult to explain this. But I can try:

At runtime, b refers to an object of class B. Since B implement I, (I)b is valid as compile as well as runtime. Let's calls (I)b as i.
Now, since I is an interface, (B)i is valid at compile time and is valid at runtime because i indeed points to an object of class B. Let's call (B)i as x.
a = x is valid at compile time because x is of type B and B is a A. It is also valid at run time because x indeed points to an object of class B.


a.b1() won't work because the class of reference a is A, which does not have the method b1().


I would suggest you refer to a book to understand this better.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

Ok, I understand this is not a cast from a to B...
Thanks for explanation.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

Hello,

I try to get my finger behind casting but not 100% sure I get it...

In below code, a.m1(); prints "C.m1()". In plain English it does the following:

B b = new C(); ==>
"Create a reference of type B b and assign to it (aka 'point it to') an instance of subclass C
B a = (B)(I)b; ==>
I tmp = (I)b; // Treat reference to instance C as reference to interface I;
B a = (B)tmp; // treat reference to interface I as reference to
an instance of class B (this is possible because B implements I via A);

WHY does a.m1() not give me the output "B.m1()"? if a points to instance of B it should?
======

Code: Select all

class LearnJava{
	public static void main(String[] args) {
		B b = new C();
		B a = (B)(I)b;
		a.m1();
	}
}
interface I{}

class A implements I{
    public void m1(){
         System.out.println("A.m1()");
    }
}
class B extends A{
    public void m1(){
        System.out.println("B.m1()");
    }
}
class C extends B{
    public void m1(){
        System.out.println("C.m1()");
    }
}
class D extends C{
    public void m1(){
        System.out.println("D.m1()");
    }
}

Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests