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
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

7. Inheritance and Polymorphism: Better ... > Looking for more inheritance opportu...

Looking for more inheritance opportunities

The class hierarchy is starting to shape up. We have each subclass override the makeNoise() and eat() methods, so that there’s no mistaking a Dog bark from a Cat meow (quite insulting to both parties). And a Hippo won’t eat like a Lion.

But perhaps there’s more we can do. We have to look at the subclasses of Animal, and see if two or more can be grouped together in some way, and given code that’s common to only that new group. Wolf and Dog have similarities. So do Lion, Tiger, and Cat.

  1. Look for more opportunities to use abstraction, by finding two or more subclasses that might need common behavior.

    We look at our classes and see that Wolf and Dog might have some behavior in common, and the same goes for Lion, Tiger, and Cat.


  

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