Why casting to a derived class should be explicit and not implicit

Object o = new Student(); // Implicit casting

I understand this code very well, and I understand that the reference variable "o" refers to data of type "Object", and as we can see, Student extends from "Object", which means that it refers to an instance of Student. and if i write the following code:

Object x = o;

This will assign the value o to x, which means that if we follow the direction of x, we will jump to that learner object above!

My question is, why can't I write the following code?

Student x = o;

      

"o" refers to an object of type "Object" and this object assigns it an address (in memory) at "o", why can't we assign this value stored in o in x!

+3


source to share


7 replies


Object o = new Student();  //upcasting - Implicit cast
Student x = (Student) o;   //downcasting -Explicit cast

      

Acceleration . We are shorthand for referencing (by moving the inheritance hierarchy). Since Student IS-A

Object does not require explicit casting



Downcasting . We are extending reference transformation (moving down the inheritance hierarchy). Since Object can be anything, we must use an explicit cast to pass it to the student.

+3


source


There is no casting here.

As you say, Object

is the base class Student

.
This means that each instance is Student

also Object

, and we can always treat it as one.

Object o = new Student(); // A Student is an Object

      



The opposite relationship is not true, however - not all instances Object

are equal to Student

s.

Once you are assigned Student

to o

, the information that actually is is Student

not available to the compiler.
This information is available at runtime - you can tell o instanceof Student

and it will be true - but the compiler doesn't know it.

+5


source


Object o;

if (Math.random < 0.5) {
  o = new Student();
} else {
  o = new Dog();
}

      

You may not have Student x = o;

it obvious

If the dog is not a student, that's a different story.

+3


source


Java allows implicit promotion, not implicit downgrade.

Downcasting in Java is also called extension conversion, and downcasting is called downcasting.

Objects can be implicitly or explicitly supertyped. In this example, Object

is a supertype Student

.

Object o = new Student();
Object x = o; // implicit works
Object x = (Object) o; // explicit works

      

Objects cannot be implicitly subtyped and must be explicitly stripped. In this example, Student

is a subtype Object

.

Object o = new Student();
// Student x = o; // implicit doesn't work
Student x = (Student) o; // explicit works

      

Explicit and Implicit Type Casting - Herong Yang

5.1.5. Extending Link Conversion - Java Docs

5.1.6. Narrowing link conversion - Java docs

+2


source


You need to explicitly point this to Student

Student x = (Student) o;

      

The reason for this is that the compiler doesn't know if this is correct, so you need to do it as if to say "I'm sure this will work. Please trust me . "

+1


source


You can assign the Student instance to a variable declared as type Object, because each student is a kind (or specialization) of Object. Since not every object is IS A Student, the compiler refuses to assign something that it only knows as an Object instance for any Object specialization such as Student. This way you can assign an instance variable of a more general type implicitly without difficulty, but in the opposite direction the compiler wants some confidence that you want to do this, so you are explicitly omitted.

Student x = (Student)o;

      

It's just the nature of a statically typed language.

+1


source


Basically you have to check if the test passes IS-A

, you can tell Student IS-A Object

(since every class in Java is a subclass of the Object class), so the first implicit casting works.

However, you cannot tell Object IS-A Student

, because it may not be. If you don't know what it will be Student

, you can downcast with

Student x = (Student) o;

      

Just in case you make a mistake with this downcast , but you can put it in an if statement like this:

if(o instanceof Student) {
    Student x = (Student) o;
}

      

+1


source







All Articles