• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Upcasting in Java?

 
Mathew Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am kinda confused about upcasting in Java?I thought converting a subclass to a super class is not possible.

Pls advice.

Thanks
Mathew Chen
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liskov substitution principle
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean using a Number object where an Object is required (just an example)?

All classes in Java can be used wherever one of their super-classes is specifically mentioned. Since sub-classes extend a super-class, they have all the properties the super-class has (and then some). The sub-class is not actually converted, though, its additional properties are just ignored in that moment.

But that's such a fundamental (and basic) concept in Java that I am wondering if I misunderstand what you're asking?
 
Mathew Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this example?

public class ClassA1 {
public void foo(){
System.out.println("Test A");
}

}

/////////////////////

public class ClassB1 extends ClassA1 {
public void foo(){
System.out.println("Print B");
}

}

///////////////////////

public class TestA1B1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassB1 B1=new ClassB1();
ClassA1 A1 = (ClassA1) B1;
A1.foo();

}

}

The above application when run prints "Print B".What is the conceptual reason for this?I know this is pretty simple stuff for you guys.

Thanks
Mathew Chen
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java decides which instance method to call at runtime ("late binding"), based on the actual type of underlying object (not the reference type).

Hope this helps...
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassA1 A1 = (ClassA1) B1;

Just a note. There is no need to explicit type cast "(ClassA1)B1". The compiler does it implicitly for you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting doesn't change the type of an object, just the type of the reference. After the casting, the ClassB1 object will still be a ClassB1 object - it's just referenced by a ClassA1 reference variable.
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always go from specific to general without any need to typecast. For example, if you have a Vehicle class and a Car class (which is a subclass of Vechicle class), then



is perfectly legal since any car is essentially a vehicle. As Ilja mentioned, calling "car" as a vehicle doesn't change the fact that it is still a "Car". It is just seen as a Vehicle. That's all.

However, if you want to convert the Vehicle back to a car



you need an explicit typecast (called downcasting), as you don't know what the Vehicle actually is.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mathew Chen:
Hi,
I am kinda confused about upcasting in Java?I thought converting a subclass to a super class is not possible.

Pls advice.

Thanks
Mathew Chen


First off there is no "conversion" in Java per se. You are simply changing the variable type that references the same object. Second, I believe you have the concept upside down. You can always reference a sub-type using a super-type reference:

Object foo = new String("Billy Bob Jolie");

This is upcasting and it's risky:

String bar = (String)foo;

If you are just kidding about foo being a String your thread will crash.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic