<< Chapter < Page Chapter >> Page >

Cats, monkeys and whales, while diverse creatures, are all mammals. Hence to model such a system in the computer, it makes sense to make Cat , Monkey and Whale all subclasses of an abstract Mammal superclass. Each species has many behaviors (methods) but I will only concentrate on 3 in particular:

  • boolean givesMilk() : returns true if the animal can give milk to feed its young, false otherwise
  • String makeSound() : returns a String represenation of a common sound the animal makes.
  • boolean givesLiveBirth() : returns true if the animal bears live young.

In the table below are the methods and what happens when each species executes that method:

Mammal Method
boolean givesMilk() String makeSound() boolean givesLiveBirth()
Cat true "Meow" true
Monkey true "Screech" true
Whale true "[whale song]" true

We could start out with the following class implemenation ( Mammal0.java ):

Model of mammals

No common methods defined in the superclass.
Italics signify abstract methods or classes
return_value : method_name(parameter_type_#1 parameter_name_#1, parameter_type_#2 parameter_name_#2, etc)

Let's start our analysis:

  • A mammal is defined by the fact that it gives milk to feed its young. It is thus not surprising that all the givesMilk() methods in the subclasses return true. The givesMilk() method is a prime candidate for "hoisting" up into the Mammal superclass ("hoisting" = moving the method upwards from the subclass to the superclass).
  • makeSound() returns a different value for each species, but intrisically, we expect any animal, which includes mammals, to be able to make some sort of sound. Thus Mammals should have a makeSound() method, but since, at the Mammals level, we don't know exactly how that sound will be made, the method at that level must be abstract. The makeSound() method at the concrete Cat , Monkey and Whale level however, would be concrete because each animal makes its own unique sound.
  • givesLiveBirth() returns exactly the same value for all of our rather diverse selection of animals here. It seems like a fine candidate for hoisting. Or is it....? Let's go ahead an hoist it anyway.

This is what we have so far ( Mammal1.java ):

Model of mammals

Abstract and common methods hoisted to the superclass.

Before we go charging ahead, let's stop for a moment and review what we've done: Cats, monkeys, and whales do represent a wide spectrum of mammals, but remember, the abstract Mammal class is a representation of ALL mammals, not just the ones we have so far. The correlation of like behavior with all our represented animals does not imply its inclusion in their abstract representation!

For instance, one day, in our wanderings through Australia, we encounter a Duckbilled Platypus . Let's see how it behaves with respect to our 3 methods:

Mammal Method
boolean givesMilk() String makeSound() boolean givesLiveBirth()
Duckbilled Platypus true "growl" false

Duckbilled platypus lay eggs!!

Giving live birth is not part of the definition of a mammal. On the other hand, the question of whether or not the animal gives live birth can always be asked of any animal, including all mammals. The result may be true or false however, so the method must be abstract at the Mammal level.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask