Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance question (K&B7, Practice exams, Objective 7.0 - Inheritance)

 
Akshaya Thirusangu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just am looking for a perfect explanation pertaining to inheritance IS-A test for this question.
I understand F5 and F6 will compile and run.
F4, F7, and F8 will not compile.
and why does F2 and F3 throw ClassCastException in Runtime?

Please help.
The question is from (Objective 7.0 - Inheritance) OCAJP IZ-803 CD material that has the pool of Practice questions.
Inheritance-test.PNG
[Thumbnail for Inheritance-test.PNG]
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
F1, F2 and F3 throw classcastexception. F5 and F6 compile and run. F4, F7,F8 does not compile.

F1 - compiles because you can cast an interface to a class reference(only if the class it is not final). But throws a class cast Exception, because Breakable is not implemented, something like line1.
F2 and F3 - compiles but throws a classCast Exception because you are downcasting to the subclass SuperFancyCar. something like line2.



F4. ((FancyCar)car), though it is a valid cast. there is no turn() method in FancyCar, so does not compile.

F5. ((FancyCar)car), it is a valid cast. it has access to breakSmoothly() method of FancyCar, so compiles and runs.

F6. compiles and runs because Car class has Accelerate() method and calls it.

F7. doesnt compile because Car class does not have turn() method.

F8. doesnt compile because Car class does not have breakSmoothly() method.

Someone correct, if anything wrong.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:F1, F2 and F3 throw classcastexception. F5 and F6 compile and run. F4, F7,F8 does not compile.

Spot-on! Have a cow for such a great post!

Ramya Subraamanian wrote:F1 - compiles because you can cast an interface to a class reference(only if the class it is not final). But throws a class cast Exception, because Breakable is not implemented, something like line1.

Although the result (a ClassCastException will be thrown at runtime) is correct, the first part is incorrect. In F1 you are not casting an interface to a class reference, it's actually the other way around. In F1 you are trying to cast the reference variable car to the interface Breakable.

Ramya Subraamanian wrote:F2 and F3 - compiles but throws a classCast Exception because you are downcasting to the subclass SuperFancyCar. something like line2.

This explanation is incomplete! You don't get a ClassCastException at runtime because you are downcasting to subclass SuperFancyCar. If the reference variable car is referring to an object of type SuperFancyCar, that statement would compile and run successfully. Unfortunately the type of the object is FancyCar and FancyCar IS-NOT-A SuperFancyCar, therefore you'll get a ClassCastException at runtime.

Ramya Subraamanian wrote:F4. ((FancyCar)car), though it is a valid cast. there is no turn() method in FancyCar, so does not compile.

F5. ((FancyCar)car), it is a valid cast. it has access to breakSmoothly() method of FancyCar, so compiles and runs.

F6. compiles and runs because Car class has Accelerate() method and calls it.

F7. doesnt compile because Car class does not have turn() method.

F8. doesnt compile because Car class does not have breakSmoothly() method.

All these statements are completely correct! One small minor nitpick about F6: the Car class has an accelerate() method, not an Accelerate() method. As you know, Java is case-sensitive

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshaya Thirusangu wrote:I understand F5 and F6 will compile and run.
F4, F7, and F8 will not compile.
and why does F2 and F3 throw ClassCastException in Runtime?

Let's start with a few (simple) observations:
  • the reference variable car is of type Car
  • the reference variable car refers to an object of type FancyCar


  • Secondly here is one very, very, very important rule you should remember: the compiler doesn't execute any code at all! So the compiler doesn't know (and doesn't care) the type of the object to which the reference variable car is referring to. The only thing the compiler knows is the type of the reference variable car (which is Car).

    Now let's have a close look at any of the statements. Fasten your seatbelt, here we go! Let's start with the easiest ones:The compiler knows Car is the type of the reference variable car, so it verifies the class Car to see if these methods are defined in this class. Only the accelerate() method is defined in this class, the other methods aren't. Therefore F6 compiles (and run) successfully, F7 and F8 give a compiler error.

    Next we have a look at these statementsIn these statements the reference variable car is cast to Breakable and SuperFancyCar in F1 and F2+F3 respectively. F1 is a valid cast (no compiler error), because you can always cast to an interface (if the class is not marked final). F2 and F3 are valid casts too (no compiler error), because Car and SuperFancyCar belong to the same class hierarchy and therefore are compatible types. Then the compiler checks if the invoked methods are defined in the Breakable interface and the SuperFancyCar class. The Breakable interface defines a breakSmoothly() method. The SuperFancyCar class defines turn() and breakSmoothly() methods. Therefore these three statements compile without any compiler error.
    When these statements are executed, the JVM will try to perform the cast. If the object is not assignable to the cast type, the cast is not ok and a ClassCastException is thrown at runtime; otherwise the cast is ok and no runtime exception is thrown. Back to the statements now, first F1. The reference variable car refers to a FancyCar object and FancyCar IS-NOT-A Breakable (FancyCar does not implement Breakable), and thus the cast is not ok. And it's very similar for F2 and F3: the reference variable car refers to a FancyCar object and FancyCar IS-NOT-A SuperFancyCar (FancyCar does not extend SuperFancyCar), and thus the cast is not ok. Therefore these three statements will throw a ClassCastException at runtime.

    Finally let's discuss the two remaining statements:F4 and F5 are valid casts (no compiler error), because Car and FancyCar belong to the same class hierarchy and therefore are compatible types. Then the compiler checks if the invoked methods are defined in the FancyCar class. The FancyCar class does not define a turn() method and that's why F4 gives a compiler error. The FancyCar class does define a breakSmoothly() method, so F5 compiles successfully. When you execute F5, the JVM will try to perform the cast. The reference variable car refers to a FancyCar object and FancyCar (of course) IS-A FancyCar, thus this cast is ok. Therefore F5 will run successfully too.

    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akshaya Thirusangu wrote:I just am looking for a perfect explanation pertaining to inheritance IS-A test for this question.

    Luckily for you, the "IS-A test (and casting + instanceof)" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "IS-A test (and casting + instanceof)". These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls) about IS-A (and casting + instanceof):
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class

  • I think you know what to do today

    Hope it helps!
    Kind regards,
    Roel
     
    Akshaya Thirusangu
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Ramya and Roel.

    Roel, Your explanation have given me complete understanding on inheritance mechanism. Please keep doing this! Great Job!

    Kind Regards,
    Akshaya
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akshaya Thirusangu wrote:Roel, Your explanation have given me complete understanding on inheritance mechanism. Please keep doing this! Great Job!

    Thanks for these kind words! I'll try to keep doing a great job
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic