By ipkiss

2012-03-19 13:15:41 8 Comments

I have the following code:

class A {

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

According to Java rule:

Case 1:

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

Case 2:

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

According to Java rule, Case 1 is ok and Case 2 is not ok. Why Case 2 is not ok? And what this line ((B)a).fB(); actually does (I mean what happens inside)?


@Jesper 2012-03-19 13:21:50

Why case 1 is OK and case 2 is not OK: Because a Dog is an Animal, but not every Animal is a 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 to the object; in particular, it doesn't do any kind conversion of objects. Casting is only telling the compiler "I have this object here, and I know better than you what it is; I want you to treat it as type X and not give 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 a Dog; it only knows that it is an Animal - and not all Animals are Dogs. You can do a cast to tell the compiler to not 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 is really a Dog, and if it isn't, you get a ClassCastException.

(And the question in your title is exactly the opposite of what you mean, as biziclop noticed).

@ipkiss 2012-03-19 14:21:13

Ok, that's clear. Just one more thing I want to ask: what is the purpose of doing thing such as: Animal pet1 = fifi;?

@Louis Wasserman 2012-03-19 18:53:18

If you have some operation you can perform with any Animal, then that can also be done with fifi.

@AlexR 2012-03-19 13:29:08

Ok, think about 2 people: Doctor and Engineer. Doctor can treat, engineer can build. Both are persons.

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 to be treated by person that was "cast" to be a doctor you do you prefer the real Doctor?

I believe that the answer is clear. Each doctor is a person (case 1) but not each person is a doctor because not each person can treat. Exactly the same is relevant for class hierarchy too. Subclass inherits its super class' properties and probably adds its own. Therefore not any instance of superclass can be cast to subclass.

@mohit jain 2012-03-19 13:24:18

Suppose case 2 is also working, then your compiler sees "b" as a object of class "B". Now you can say "b.fb()". But actually "b" is an object of "A"(Remember you assigned an object of class "A" to "b"). there is no function fb() in class "A". your application crashes!!

@miniBill 2012-03-19 13:19:30

Case 1 is ok because with the (B)a part you are explicitly telling the compiler:

even if you only know that a is of type A I am telling you that it is of type B

so the object a is treated as if it were of type B

Case 2 is not ok because the compiler can't safely assign to b to a. It would be accepted if you wrote

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

but it would throw an Exception at runtime

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

30 Answered Questions

[SOLVED] How to avoid Java code in JSP files?

  • 2010-07-05 07:24:06
  • former
  • 259712 View
  • 1572 Score
  • 30 Answer
  • Tags:   java jsp scriptlet

34 Answered Questions

16 Answered Questions

12 Answered Questions

[SOLVED] Why does Java have transient fields?

  • 2009-05-26 12:11:36
  • Animesh
  • 612489 View
  • 1275 Score
  • 12 Answer
  • Tags:   java field transient

32 Answered Questions

[SOLVED] Can't execute jar- file: "no main manifest attribute"

1 Answered Questions

[SOLVED] Why does array[idx++]+="a" increase idx once in Java 8 but twice in Java 9 and 10?

8 Answered Questions

24 Answered Questions

[SOLVED] Why can't I define a static method in a Java interface?

19 Answered Questions

[SOLVED] How do I "decompile" Java class files?

  • 2008-11-07 16:00:03
  • Kip
  • 679991 View
  • 556 Score
  • 19 Answer
  • Tags:   java decompiler

Sponsored Content