Domanda

I have a problem in understanding Question no.9 from Chapter Object Orientation from the SCJP book by K&B.

Question:

public class Redwood extends Tree { 

public static void main (String [] args) { 
new Redwood ( ) . go ( ) ; 

} 

void go ( ) { 

go2 (new Tree ( ) , new Redwood ( ) ) ; 

go2 ( (Redwood) new Tree ( ) , new Redwood ( ] 

}  

void go2 (Tree tl, Redwood rl) { 

Redwood r2 = (Redwood) tl; 

Tree t2 = (Tree)rl; 
}
}


class Tree { } 

Options:

What is the result? (Choose all that apply.) 

A. An exception is thrown at runtime 

B. The code compiles and runs with no output 

C. Compilation fails with an error at line 8 

D. Compilation fails with an error at line 9 

E. Compilation fails with an error at line 12 

F. Compilation fails with an error at line 13 

The answer given in the book is A because Tree cannot be downcast to Redwood. I am just having problem to understand the concept.

È stato utile?

Soluzione 3

A Child class instance can be cast to Parent class reference, because since Child inherits Parent , Child should support all behaviors which Parent already supported.

Example

class Parent {

   public void getA() {
        return 1;
   }

}

class Child extends Parent {


   public void getB() {
       return 1;
   }

}

Now lets consider 2 objects

Child c = new Child();
Parent p = new Parent();

Now if you do

Parent p2 = (Parent) c;

this is valid, because when you invoke p2.getA(), it will work since getA() method is already inherited by c which is instance of Child.

Now if you do

Child c2 = (Parent) p;

this will not work because, the type of c2 is Child, and the call c2.getB() is valid. But since the instance p is of type Parent, it doesn't have implementation to getB() (which was the new method added down in the inheritance by child class).

In simple terms inheritance is an IS A relationship

Coming back to your problem

A Redwood IS A Tree so Tree t = (Tree) (new Redwood()); works. This means a Redwood instance can be cast to Tree

But a Tree is NOT A Reedwood always (it can be anything).. so Redwood r = (Redwood) new Tree() doesn't work

Altri suggerimenti

>

class Tree{
   // super class
}

public class Redwood extends Tree{
   //child class
   public static void main (String [] args) { 
   new Redwood ( ) . go ( ) ; // Calling method go()
    } 

    void go ( ) { 

   go2 (new Tree ( ) , new Redwood ( ) ) ; 

   go2 ( (Redwood) new Tree ( ) , new Redwood ( )); // Problem is Here

   /*
   (Redwood)new Tree(),------>by this line Tree IS-A Redwood Which wrong


   According to Question
     Redwood IS-A Tree So relationship is
     (Tree) new Redwood();


   */

  } 

  void go2 (Tree tl, Redwood rl) { 

  Redwood r2 = (Redwood) tl; 

  Tree t2 = (Tree)rl; 
}

This line will throw the exception at runtime :

go2 ( (Redwood) new Tree ( ) , new Redwood ( ));

Because you are casting a Tree object to Redwood which is not possible

Your Tree class is the parent class and you cannot down-cast a parent class object to a child class object.

This is not valid :

(Redwood) new Tree ( )

But the reverse is true.

That is this is perfectly valid :

(Tree) new redwood ( )

if you pass a tree object as following then it is legal

Tree t1 = new Redwood ();

Because Tree can be Redwood or an some tree ... so you can't downcast at run time

Option A : An exception is thrown at runtime

because of go2 ( (Redwood) new Tree ( ) , new Redwood ( ] //runtime exception

Becasue you are casting Tree object to Redwood object and its not possible. Your Tree class is the parent class and you cannot down-cast a parent class object to a child class object.

ClassCastException will be thrown when the code attempts to downcast a Tree to a Redwood. So the correct answer is: A

That code doesn't compile due to obvious syntax errors: The ] in the go method has no matching [, and the ' after the go method has no matching ' either. Additionally, the code lacks two }.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top