wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help on Assigning one Reference to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help on Assigning one Reference to another" Watch "Help on Assigning one Reference to another" New topic
Author

Help on Assigning one Reference to another

kirti tiwari
Greenhorn

Joined: Feb 04, 2007
Posts: 15
Hi Ranchers,

I am pretty confused in scenarios in which superclass ref is assigned to subclass or vice-versa or say objects assignments are being done in context of inheritance??

Sometimes it is compiler error, sometimes runtimeerror.....

Can anybody help me out by stating example for the possible scenarios--when does it compiler error and when is it runtime???

I will be greatful.

Thanks
Kirti
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
Subclass can be assigned to a supertype without any explicit casting.

But if we want to assign a superclass to a subtype , we need to cast it explicitly. Otherwise it will result in compilation error.


When we assign a supertype to a subtype after casting, the compilation process is success.But it can cause runtime exception if the supertype doesn't point to the specified subtype at runtime.


SCJP 1.5<br />SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Almost Priya. The compiler can refuse to cast if it is clear that the cast cannot succeed because the classes are not in the same inheritance hierarchy.

Does anyone remember the rule for casting to an interface?
[ February 06, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
You can cast any object to an interface type, provided the class is not final.

If S is a class type: ... If T is an interface type: ... If S is not a final class, then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
and
"If S is a class type: ... If T is a class type, then S and T must be related classes-that is, S and T must be the same class, or S a subclass of T, or T a subclass of S; otherwise a compile-time error occurs."
preethi venkatarangan
Greenhorn

Joined: Jan 21, 2007
Posts: 27
Originally posted by Priya Viswam:
Subclass can be assigned to a supertype without any explicit casting.

But if we want to assign a superclass to a subtype , we need to cast it explicitly. Otherwise it will result in compilation error.


When we assign a supertype to a subtype after casting, the compilation process is success.But it can cause runtime exception if the supertype doesn't point to the specified subtype at runtime.


can you an example so that it can even more clear..it would be really helpful for me..
kirti tiwari
Greenhorn

Joined: Feb 04, 2007
Posts: 15
But it can cause runtime exception if the supertype doesn't point to the specified subtype at runtime.


Please let me know what does that mean actually....With the help of syntax?

Thanks
Kirti
kirti tiwari
Greenhorn

Joined: Feb 04, 2007
Posts: 15
Thank you all for help....i myself got it....:-)
class base
{
}
class sub extends base
{
public static void main(String args[])
{
base b=new base();
sub s=new sub();
s=(sub)b;
}
} /* will compile fine but will fail during runtime as base object is not pointing to sub object.*/

It will work fine even during runtime if base class reg points to subclass object.

class base
{
}
class sub extends base
{
public static void main(String args[])
{
base b=new sub();
sub s=new sub();
s=(sub)b;
}
}

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help on Assigning one Reference to another