Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

7. Inheritance and Polymorphism: Better ... > Designing an Inheritance Tree

Designing an Inheritance Tree

image with no caption

Sharpen your pencil

Draw an inheritance diagram here.

Find the relationship that make sense. Fill in the last two columns

Class

Superclasses

Subclasses

Musician

  

Rock Star

  

Fan

  

Bass Player

  

Concert Pianist

  

Hint: not everything can be connected to something else. Hint: you’re allowed to add or change the classes listed.

There are no Dumb Questions

Q:

Q: You said that the JVM starts walking up the inheritance tree, starting at the class type you invoked the method on (like the Wolf example on the previous page). But what happens if the JVM doesn’t ever find a match?

A:

A: Good question! But you don’t have to worry about that. The compiler guarantees that a particular method is callable for a specific reference type, but it doesn’t say (or care) from which class that method actually comes from at runtime. With the Wolf example, the compiler checks for a sleep() method, but doesn’t care that sleep() is actually defined in (and inherited from) class Animal. Remember that if a class inherits a method, it has the method.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint