Win a copy of Fixing your Scrum this week in the Agile forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Rob Spoor
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Holloway
• Piet Souris
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Frits Walraven
• Himai Minh

# examlab - 1 [reference variable casting]

Ranch Hand
Posts: 198
• Number of slices to send:
Optional 'thank-you' note:

Can somebody explain the thought process for this question? [the six lines 'from here']
Thanks

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
not that i have taken this exam, but coming from a math background this concept is used frequently in group and set theory. this is what is known as the Property of Transitivity.

if A and B extends A, then A is contained in B. if B and C extends B, then B is contained in C. therefore by transitivity, A is contained in C. hence in C, B implements Min. (def of class.... all in the same group,,, much like the Reals or some other set or subset you might be working with)

since B also implements Min, you can cast "m" to C because B is contained in C and B implements Min. (//from here)
m is Class Min which is implemented by B. etc. etc.

dont bother try to run this... it does nothing except demonstrate concept... you need to actually create and A B and C class that does something in order to really see it at work.

Ranch Hand
Posts: 808
1
• Number of slices to send:
Optional 'thank-you' note:
instanceof test fails but no ClassCastException is thrown

Ranch Hand
Posts: 157
• Number of slices to send:
Optional 'thank-you' note:

Aakash Goel
Ranch Hand
Posts: 198
• Number of slices to send:
Optional 'thank-you' note:
I still do not know how to think about these questions.

There are two parts to the problem:
a) whether it will compile
b) whether it will run

and in solving these type of problems I am always confused whether to look at the reference first or the object?

Ranch Hand
Posts: 814
• Number of slices to send:
Optional 'thank-you' note:
Hi Aakash,

Up cast means casting from sub class to super class
Down cast means casting from super class to sub class

Up casting happen automatically or you can do it manually.
Down casting happen only manually. You have to do it.

See comment at end of each line

Correct me if I am wrong

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:

Up cast means casting from sub class to super class
Down cast means casting from super class to sub class

Up casting happen automatically or you can do it manually.
Down casting happen only manually. You have to do it.

See comment at end of each line

Correct me if I am wrong

This will throw runtime ClassCastExceptions right??

Aakash Goel
Ranch Hand
Posts: 198
• Number of slices to send:
Optional 'thank-you' note:

thanks for the excellent explanation [now atleast i will think of attempting such questions]

So, in this situation the casts compile and run.

It would be helpful if you can give a case where the cast compiles but not runs.

Aakash Goel
Ranch Hand
Posts: 198
• Number of slices to send:
Optional 'thank-you' note:

aabir sanyal wrote:
This will throw runtime ClassCastExceptions right??

No, they all run fine. --> No runtime exceptions.

It seems I am not the only one confused with this.

I am waiting for someone to give an example where they compile but fail at runtime.

Ranch Hand
Posts: 41
• Number of slices to send:
Optional 'thank-you' note:
No there is no ClassCastException. It Compiles and run fine. I have a confusion that Min is an interface we are supposed not to create object of interface.
so can any one explain me What this Line is doing here..??

@Aakash

Thanks & Regards
Jeetendra..!!

Aakash Goel
Ranch Hand
Posts: 198
• Number of slices to send:
Optional 'thank-you' note:

jeetendra Choudhary wrote:No there is no ClassCastException. It Compiles and run fine. I have a confusion that Min is an interface we are supposed not to create object of interface.
so can any one explain me What this Line is doing here..??

@Aakash

Thanks & Regards
Jeetendra..!!

That line initializes the m reference to null. The only restriction is that you cannot use the new construct with an interface type.

I think I now understand how this happens.

avi sinha wrote:
what i know is:

1> you can cast object refernces only if they are in the same inheritance tree. you can ,means it will compile .it doesn't say that it will not give any runtime error.

2> if the original object & the referance are of the same type ,it will run fine too.

3>if the original object is an object of the subclass of the referance type ,it will run fine too.

avi sinha

----------------------------------------------------------------------------------------------------------------------------------------------

Related to this :

does anyone of the following have anything to do with the concepts of reference variable casting?

a) instanceof
b) ==
c) equals()

Now that we know how casting works, probably understanding these will be easier?

jeetendra Choudhary
Ranch Hand
Posts: 41
• Number of slices to send:
Optional 'thank-you' note:
Well What i know is
instanceof() is used to check the ISA relation that is the object is an object of the subclass of the reference type or not.
==
is used for primitive comparison. it becomes a little tricky when it comes to wrapper class except that it returns true if the primitives having equal value.
equals()
is used for object comparison. means wheather both object reference refer to same object or not.

Any more explanation and rules are very welcome.

Thanks & Regards.
Jeetendra...!!

Ranch Hand
Posts: 814
• Number of slices to send:
Optional 'thank-you' note:
@ Aakash,

Object o = new Obejct();
String s = new String("String");
Integer i = new Integer(1);

o = s; It will compile and runs fine. Automatic upcast.

s = (String)o; It will compile and runs fine. Manual downcast needed.

s = (String)i; It will not compile. Compiler thinks that Integer and String are different objects, they can't refer to each other always so compiler will not allow at compile time.

o = s; Automatic Upcasting allowed.
i = (Integer)o; It will compile but throws ClassCastException here. Actually o refer to String object but compiler thinks that o may refer to Integer object so compiler will allow this code to compile.

Correct me if I am wrong

Ranch Hand
Posts: 317
• Number of slices to send:
Optional 'thank-you' note:

Object o = new Obejct();
String s = new String("String");
Integer i = new Integer(1);
o = s; It will compile and runs fine. Automatic upcast.
s = (String)o; It will compile and runs fine. Manual downcast needed.

You are right, if you have assigned before the reference variable o the String object s, otherwise the execution will fail. It needs to be assigned to a String object. So the following code doesn't execute, but successfully compiles:

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
The last post is confusing me.

It was said:

but now there is a similar situation with casting Object to Integer
i = (Integer)o
and when casting Object to String since both String & Integer are subclasses of object then why doesnt the above formula for A and m work here??

I know I am wrong somewhere in understanding this, but can anybody tellme?

[AG Edit: corrected code tags]

Ranch Hand
Posts: 537
• Number of slices to send:
Optional 'thank-you' note:

This will compile fine but execute only if m has the object of B or C.

This will compile fine but execute only if o has an Integer Object

Both cases, compilation is successful but there is a possibility of choking at runtime.

Ranch Hand
Posts: 129
• Number of slices to send:
Optional 'thank-you' note:

Nitish Bangera wrote:

This will compile fine but execute only if m has the object of B or C.

This will compile fine but execute only if o has an Integer Object

Both cases, compilation is successful but there is a possibility of choking at runtime.

this also compiles and runs fine though I commented m having a reference to b. Please explain what you mean.

Nitish Bangera
Ranch Hand
Posts: 537
• Number of slices to send:
Optional 'thank-you' note:
Well basic point to note is that the compiler will check only the reference type and while casting it checks the the variable which is present for casting to the casted type is in the same inheritance hierarchy.

Well m is an interface and can have Objects of type B and C. So it can be casted to C and given to and reference type of B. This will only choke at runtime if the object present is of type B which cannot be casted to the type C. So this gives a runtime exception. But in your original code Min m = null means its not assigned to any object at runtime. So there is no exception at runtime as all the references are nulled which is valid.

Well this will compile and execute properly without any exception even if m referred to an object of Type B and C. But in your code m = null so a2 is assigned a null reference.

Same goes for

But in

If m hadn't been null then this particular would give a runtime exception if b2 referred to an object of type B which could not be casted to a type of C. But as m = null, m2 is assigned null.

On the same lines is

Sheriff
Posts: 9686
42
• Number of slices to send:
Optional 'thank-you' note:
Something similar on this line has been discussed here too. And remember, null can be typecasted to any type...

S Ali
Ranch Hand
Posts: 129
• Number of slices to send:
Optional 'thank-you' note:
So compiler only cares about reference variable types ,and that they are the same hierarchy without looking at what the reference contains.
And whether this object is compatible with the cast or not is only determent at runtime. Did I get it right???
If so will I be expected to know when it throws exceptions?

Nitish Bangera
Ranch Hand
Posts: 537
• Number of slices to send:
Optional 'thank-you' note:
The JVM creates the object and not the compiler. So the objects are present only at runtime. Compiler's present only to check if the syntax typed by the programmer are correct or no and also if the particular things will cause an error at runtime as in with generics.

You will know when it will throw an exception if the reference type refers to an object which cannot be casted to the type given by the programmer.

S Ali
Ranch Hand
Posts: 129
• Number of slices to send:
Optional 'thank-you' note:
Does it ask at the exam if it fails to run? Should I be expected to know that

Nitish Bangera
Ranch Hand
Posts: 537
• Number of slices to send:
Optional 'thank-you' note:
I guess its pretty basic to know that..... Well K&B chapter 5 pg 382 will help you. Know what all exceptions are there and when are they thrown. Well Compilation fails and Runtime exceptions are some of the answers in the SCJP6 exam so you need to know them.

S Ali
Ranch Hand
Posts: 129
• Number of slices to send:
Optional 'thank-you' note:
Thanks, I guess I do have to read the book again.

 Politics is a circus designed to distract you from what is really going on. So is this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising