Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 6 ■ OBJECT-ORIENTED PROGRAMMING


computer science. Each field defines it in terms relevant to that field of study, but it all boils down to
having multiple forms.
In object-oriented computer languages, one kind of polymorphism is the ability of different classes
to respond appropriately to the same input. So, our Mammal class might define a speak method. The Cat
class would implement the speak method as a meow, the dog as a bark, and the mouse as a squeak. They
all implement the same method, but they each do it in their own appropriate way. These methods are
said to be overridden. The methods in the child class replace the behavior of the parent class with their
own behavior—that is, they override the parent's method.
Another kind of polymorphism (called overloading) is having the same behavior mean something
different depending on the object passed to the behavior (or method). For example, our Cat object might
have two methods called chase, and an instance of Cat would exhibit different behavior for the
chase(Tail) method and the chase(Mouse) method. That is, we have methods of the same name, but
they have different signatures (one takes a Tail object as its argument and one takes a Mouse object).
That's a classic example of method overloading.

Our Animals in Java


Listing 6-7 shows an incomplete definition of the animal classes used as examples earlier in the chapter.
When a developer writes this kind of code, the developer is “creating stubs” or “writing stubbed out
code.” A stub means that we created the structure but have left at least some of the details for another
time or for someone else to do.
You should put your code in packages. That way, the code in one package won't interfere with the
code in other packages. If you put everything in the default package, you soon run into the problem of
wanting to call a class or interface by a name you already used. I suggest you get in the habit of putting
all your code into packages. The package used throughout this chapter includes a Predator interface.
Imagine if want another object model that defined Predator drones (the remote-control aircraft made
famous in recent wars). That's exactly the kind of entanglement that makes packages so useful. So we
add a package definition for each class and interface. In this case, they are in the same package.

Listing 6-7. The finished Mammal class

package com.apress.java7forabsolutebeginners.examples.animalKingdom;

abstract class Mammal {
// And here's a method for making the sound.
// Each child class must implement it.
abstract void speak();

// All descendant classes can call this
// and do not need to implement their own versions of it
protected void sayWhatIAm() {
System.out.println("I am a mammal");
}
}

The Mammal class serves as the basis for our other classes. As we discussed previously, we never want
to create a Mammal object, so it's abstract. It also has an abstract method, so every class that extends
Mammal has to implement a speak method with the same signature (returns void and accepts no
arguments). Finally, it includes an ordinary method, which descendant classes can use (see Listing 6-8).
Free download pdf