Explicit Type Conversion of sub class object to super class in java
Solution 1
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)
Solution 2
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{
}
Solution 3
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
Solution 4
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.
Solution 5
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.
Ankur
I am a masters student at Stony Brook University. I love programming and love to learn new things. I believe, to learn a programming language to need be thorough in core basics, then advanced version is going to be a piece of cake :) Favourite quote: When life gives you lemon make lemonade and party hard!!! My Profiles linkedin: http://www.linkedin.com/in/ankurmittal1989 GITHUB: https://github.com/ankurmittal #SOreadytohelp
Updated on June 09, 2022Comments
-
Ankur almost 2 years
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?
-
Ankur over 11 yearsSo the first statement is redundant? there is no scenario in java where you would need it? right?
-
Rohit Jain over 11 years@Ankur. Exactly. a super class reference can point any of its subclass object.. Thats the whole point of Polymorphism.
-
Ankur over 11 yearsfinally I found a scenario in which I might need to use it, thanks you so much...
-
Bhesh Gurung over 11 yearsIMO, the OP has clearly mentioned - going from subclass to superclass. Not an invalid point though.