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

Hour 4. Object-Oriented Programming with... > Extending Objects Through Inheritanc...

Extending Objects Through Inheritance

So far we’ve only been working with a single class, Monster. What if we want to have different kinds of monsters and we want them to behave differently, but we also want them to inherit common functionality?

In other words, suppose we have a werewolf who does everything that a monster does, except that when the growl method gets called on a werewolf, he howls. Additionally, suppose we have a serpent and when the growl method gets called, he hisses. Other than having their own custom growls, both serpents and werewolves should behave just like other monsters.

Perform the following steps so that we can experiment with inheritance:

1. Follow the steps in the New File wizard like you did to create Monster, but create a new empty class called Werewolf. When prompted for what class this one inherits from, enter Monster, as shown in Figure 4.4.


  

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