• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help on Assigning one Reference to another

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic