Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

examlab - 1 [reference variable casting]

 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code compiler and runs! I have no idea how to start thinking about this question.

Can somebody explain the thought process for this question? [the six lines 'from here']
Thanks
 
nathan dennis
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instanceof test fails but no ClassCastException is thrown
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code, Lukas.
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Please help me with this
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
aabir sanyal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote: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



This will throw runtime ClassCastExceptions right??
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ninad,

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Eclipse IDE Mac OS X Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
i think This Link would be helpful.

Thanks & Regards
Jeetendra..!!
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
i think This Link would be helpful.

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.

Thanks for the link:

I think I now understand how this happens.

I am crossposting a reply from the link mentioned by Jeetendra:

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
Eclipse IDE Mac OS X Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...!!
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:@ 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.


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:
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.


 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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





 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it ask at the exam if it fails to run? Should I be expected to know that
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I guess I do have to read the book again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic