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 ... > Using inheritance to avoid duplicati...

Using inheritance to avoid duplicating code in subclasses

We have five instance variables:

picture – the file name representing the JPEG of this animal

food – the type of food this animal eats. Right now, there can be only two values: meat or grass.

hunger – an int representing the hunger level of the animal. It changes depending on when (and how much) the animal eats.

boundaries – values representing the height and width of the ‘space’ (for example, 640 x 480) that the animals will roam around in.

location – the X and Y coordinates for where the animal is in the space.

We have four methods:

makeNoise () – behavior for when the animal is supposed to make noise.

eat() – behavior for when the animal encounters its preferred food source, meat or grass.


  

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