Reputation: 12774
Consider below code:
public class Test{
public static void main(String str[]){
B b = new B();
A a1 = (A)b;//Explicit type conversion
A a2 = b;
}
}
class A{}
class B extends A{}
In the above code are the two line:
A a1 = (A)b;//Explicit type conversion
A a2 = b;
Equivalent? If not then what is the difference between the two and if yes then is there any scenario in java where we need to explicitly convert a sub class object into a super class object?
Upvotes: 5
Views: 16156
Reputation: 1249
Such type conversion is also required for method selection using overloading :
package casting;
public class ImplicitCasting {
public static void main(String[] args) {
A a = new A();
B b = new B();
A a1 = new B();
methodA(a); // methodA called
methodA((A)b); // methodA called
methodA(b); // methodB called
}
public static void methodA(A a) {
System.out.println("methodA called");
}
public static void methodA(B b) {
System.out.println("methodB called");
}
}
class A{
}
class B extends A{
}
Upvotes: 3
Reputation: 51030
Equivalent? If not then what is the difference between the two
The only different is that one is implicit while other is explicit (which is not required), The result is equivalent. Note: The casting works on reference to the object, not on the object itself.
is there any scenario in java where we need to explicitly convert a sub class object into a super class object?
Java supports Liskov substitution principle, so there shouldn't be any scenario like that.
Upvotes: 1
Reputation: 21
They are both the same. This is a case of downward typecasting which is automatic.
A a1 = (A)b;//Explicit type conversion A a2 = b;
In both cases type of a1 and a2 is A. So additional characteristics of B is anyway lost.
You will know the difference if you do upward typecasting which is not Automatic. consider following example.
Vehicle v1 = new Car(); //Right . upcasting or implicit casting
Vehicle v2= new Vehicle();
Car c0 = v1; // Wrong compile time error "Type Mismatch" //Explicit or downcasting is required Car c1 = (Car) v1 // Right. downcasting or explicit casting . v1 has a knowledge of Car due to line 1
Car c2= (Car) v2; //Wrong Runtime exception ClassCastException because v2 has no knowledge of Car.
Bus b1 = new BMW(); //Wrong compile time error "type mismatch"
Car c3 = new BMW(); //Right. Upcasting or implicit casting
Car c4 = (BMW) v1; // Wrong Runtime exception ClassCastexception
Object o = v1; //v1 can only be upcast to its parent Car c5 = (Car) v1; // v1 can be downcast to Car due to line 1
Upvotes: 0
Reputation: 34367
They are not equivalent in your example.
This becomes important in other way assignment i.e. from A
to B
while your object is still type B
e.g. consider the sequence below:
A a = b;// will work
a = (A)b;// will work
B newB = (B)a; //will work
B newB = a; //will fail
Upvotes: 1
Reputation: 213223
There is no difference between the two. In fact, you don't need to type cast explicitly from a subclass object to a super class reference
. So, the first way is absolutely Redundant
.
From the JLS - Conversion: -
5.1.5. Widening Reference Conversion
A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.
Widening reference conversions never require a special action at run-time and therefore never throw an exception at run-time. They consist simply in regarding a reference as having some other type in a manner that can be proved correct at compile time.
Explicit type casting is only required at place where it is not obvious that the reference type can store the object
. But when you create an object of subclass and make reference of super class point to that object, then compiler never has an problem with that. Since that can always he handled at runtime.
Upvotes: 1
Reputation: 533492
The explicit type casting of the reference, not the object) is redundant and some IDEs will suggest you drop it.
If you do
A a1 = (A)b;
You can still do
B b2 = (B) A;
to cast the reference back to type of B.
Note: the object is not altered in any way and is always a B
there is no scenario in java where you would need it?
The only time you need an upcast is in method selection.
void method(Object o) { }
void method(String s) { }
method("hello"); // calls method(String)
method((Object) "hello"); // calls method(Object)
Upvotes: 10