File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes What is your understanding of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is your understanding of "Casting" ?" Watch "What is your understanding of "Casting" ?" New topic

What is your understanding of "Casting" ?

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
For the following code
class Super{
int a = 1;
int b = 2;
class Sub extends Super{
int a = 3;
int c = 4;
class Foo {
public static void main(String argv[]){
Sub sb = new Sub();
Super sp = (Super)sb;
System.out.println("sb.a = " + sb.a);
System.out.println("sp.a = " + sp.a);
The result is :
sb.a = 3
sp.a = 1
But I didn't create a new Super(), so where does sp.a = 1 come from ?
What happened when Super sp = (Super)sb;
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Data members can be hidden in subclasses, while methods can be overridden in subclasses. Deciding which method to call is determined at runtime, while de decision which data member must be read/altered, is decided at compile time by the compiler itself.
While you didn't explicitly constructed a Super object, it was created though, being a part of the Sub object, because a Sub object inherits from its parent class Super.
Your Sub object contains four data members, a and b from the Superclass and a and c defined in the Sub class itself. The first member 'a' is hidden by the latter member 'a'.
If you assign a Sub object to a Super variable, and later refer to member a, the compiler decides that you're refering to member a of the Super class. If you'd tried to refer to member 'c' using the Super variable, the compiler would have complained, because a Super object doesn't contain a member named 'c'. Just for the fun of it, change your classes like this:

kind regards
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
With other words: Fields aren't polymorphic, only (instance) methods are.
Encapsulate the access to the fields in accessor methods, and you will see the difference.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
It means that the Methods will be overridded in the subclasses but Fields won't .
I know it now . Thank you .
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Ths SG ,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should not be obviously fictitious. We really would prefer that you use your REAL name. Besides, I can't PRONOUNCE what you have there .
You can change your name: here.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: What is your understanding of "Casting" ?
It's not a secret anymore!