Explicit conversion of subclass object type to superclass in java

Consider the code below:

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{} 

There are two lines in the above code:

 A a1 = (A)b;//Explicit type conversion A a2 = b; 

Equivalent? If not , then what is the difference between the two, and if so , is there any script in java where we need to explicitly convert a subclass object to a superclass object ?

+4
source share
6 answers

Explicit reference of the type of the link, not the object) is redundant, and some IDEs will suggest that you delete it.

If you do

 A a1 = (A)b; 

You can still do

 B b2 = (B) A; 

to return a reference to type B.

Note: the object does not change in any way and is always B

There is no script in java, where do you need it?

The only time you need upcast is the choice of method.

 void method(Object o) { } void method(String s) { } method("hello"); // calls method(String) method((Object) "hello"); // calls method(Object) 
+9
source

This type conversion is also required to select a method using overload:

 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{ } 
+3
source

In your example, they are not equivalent.

This becomes an important other way of assignment, i.e. from A to B , as long as your object is still type B

eg. consider the following sequence:

  A a = b;// will work a = (A)b;// will work B newB = (B)a; //will work B newB = a; //will fail 
+1
source

There is no difference between them. In fact, you do not need to enter text explicitly from a subclass object to a super class reference . So, the first way is absolutely Redundant .

From JLS - Conversion : -

5.1.5. Link Conversion Extension

An expanding reference conversion exists from any reference type S to any reference type T if S is a subtype (ยง 4.10) of T.

Extending reference conversions never requires a special action at runtime, and therefore, never throw an exception at runtime. They are simply that the link refers to some other type in a way that can be proved correctly at compile time.

Explicit casting is only required where it is not obvious that the reference type can store object . But when you create a subclass object and reference the superclass object to that object, then the compiler will never have a problem with that. Because this could always be done at runtime.

+1
source

Equivalent? If not, what is the difference between the two

The only difference is that it is implicit and the other is explicit (which is not required). The result is equivalent. Note. Casting works by reference to the object, not the object itself.

Is there any script in java where we need to explicitly convert a subclass object to a superclass object?

Java supports the Liskov substitution principle , so there should not be such a scenario.

+1
source

They are both the same. This is a case of automatic type conversion down.

A a1 = (A) b; // Explicit type conversion A a2 = b;

In both cases, the types a1 and a2 are A. Therefore, the additional characteristics of B are lost anyway.

You will recognize the difference if you do an upcast cast that is not automatic. consider the following example.

Car v1 = new car (); // Correct. boost or implicit casting

Car v2 = new car ();

Car c0 = v1; // Invalid compile-time error "Mismatch Type" // Explicit or decreasing Car c1 = (Car) v1 // Right. understatement or explicit casting. v1 has car knowledge on line 1

Car c2 = (Car) v2; // Invalid Runtime ClassCastException exception because v2 does not know Car.

Bus b1 = new BMW (); // Invalid compile-time type mismatch

Car c3 = new BMW (); // Correct. Acceleration or implicit casting

Car c4 = (BMW) v1; // Invalid runtime exception ClassCastexception

Object o = v1; // v1 can only go up to the parent Car c5 = (car) v1; // v1 can be disabled for the car on line 1

0
source

Source: https://habr.com/ru/post/1439206/


All Articles