File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment of references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment of references" Watch "Assignment of references" New topic
Author

Assignment of references

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
I'm confused as to how the assignment of the references of superclass object to subclass variables and vice versa done?
Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
It is easy if you try to understand.
Conversion of instance of
Subclass -> Base allowed without any cast.
but

Base -> Subclass not allowed without cast.
(It is same like that for primitives).
Even when you cast from Base -> Subclass the subclass should actually contain the methods to be invoked using the subclass reference variable.
Search for examples you will find plenty of them,
Good luck.


"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
Hi well when u r assigning a subclass type to a superclass type u don't have to cast explicitly.
ex :
String s="hello";
Object o =s;
but when u r assigning a superclass type to a subclass type u have to explicitly cast it to a subclass type.Ex
Object o = new Object();
String s=(String) o;
But remember that in both cases the classes involved should be from the same inheritence tree. i.e. the classes must be related by inheritence.
otherwise u will get ClassCastException
 
 
subject: Assignment of references
 
Similar Threads
Anybody waiting for 'Quick Computer' Assignment Results?
HashMapping
Book Of Knowledge(BOK)
a question about arrays
Help Please