Why can't a superclass object be implicitly converted to a subclass object in Java?

I have the following code:

class A { } class B extends A { public void fB(){}; } 

According to the Java rule:

Case 1:

 B b = new B(); A a = b; ((B)a).fB(); 

Case 2:

 A a = new A(); B b = a; 

According to the Java rule, case 1 is ok and case 2 is not normal . Why is case 2 not normal ? And what this line actually does ((B)a).fB(); (I mean what's going on inside)?

+4
source share
4 answers

Why is case 1 okay, and case 2 is not OK: since Dog is Animal , but not every Animal is Dog .

 class Animal { } class Dog extends Animal { } class Cat extends Animal { } Dog fifi = new Dog(); Cat tommy = new Cat(); // OK because Dogs and Cats are Animals Animal pet1 = fifi; Animal pet2 = tommy; // Not OK because an Animal is not always a Dog Dog doggy = pet2; 

Note that casting does nothing for the object; in particular, it does not carry out any transformation of objects. Casting says only to the compiler: "I have this object here, and I know better than you what it is, I want you to treat it like type X and not tell me any error messages."

So, in a line like this:

 Dog doggy = pet2; 

the compiler will complain because it cannot be sure that pet2 is actually Dog ; he only knows that it is Animal - and not all Animal - Dog s. You can throw to tell the compiler not to complain about this:

 // Tell the compiler that you want it to treat pet2 as a Dog Dog doggy = (Dog)pet2; 

But when you run the program, Java will still check if pet2 Dog valid, and if not, you get a ClassCastException .

(And the question in your name is exactly the opposite of what you mean, as the bizicop noted).

+13
source

Well, think of two people: a doctor and an engineer. A doctor can heal, an engineer can build. Both are human.

Now here is your example.

 Person p = new Person(); // somebody that does not know to do anything special. Doctor d = (Doctor)p; // do you think that casting makes person that does not know anything to be a doctor? 

Do you want a person who has been β€œabandoned” to contact you to be the doctor you prefer to be a real doctor?

I believe the answer is clear. Every doctor is a person (case 1), but not every person is a doctor, because not everyone can treat. The same applies to the class hierarchy. The subclass inherits the properties of the superclass and probably adds it. Therefore, not every instance of a superclass can be attributed to a subclass.

+2
source

Case 1 is fine, because (B) the part that you explicitly tell the compiler:

even if you only know that a has type A, I tell you that it has type B

therefore, object a is treated as if it were type B

Case 2 is not appropriate because the compiler cannot safely assign b to a. That would be accepted if you wrote

 A a = new A(); B b = (B)a; 

but it will throw a runtime exception

0
source

Suppose 2 also works, then your compiler sees "b" as an object of class "B". Now you can say "b.fb ()". But actually, β€œb” is the object β€œA” (remember that you assigned the object of class β€œA” to β€œb”). in class "A" there is no function fb (). your application crashes!

0
source

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


All Articles