home—info—exams—lectures—labs—hws
syntax—Laws—lies—java.lang docs—java.util docs
Inheritance
We've seen class Dog, where every Dog has
a sound, and a method speak (which repeats that sound twice).
What if we had other animals we wanted to model?
Say, cats and pythons, which both had those same behaviors?
However, we will still want separate classes, since some activities
are specific to Dogs (like ageInDogYears()).
One solution would be lots of repeated code for speak.
But (for the first time)1 we'll see what object-oriented programming buys for us:
the ability to share code between different (related) classes.
- class B extends A;
we say that B is the “subclass”,
and
A is the “superclass”.
Also: Every B “is-a” A.
Note the arrow in BlueJ — different from the “has-a”
(whose arrowhead isn't closed).
(click for .jar)
-
Call polymorphically (unexciting):
// Imagine this method in some other class entirely...
static String communicateHunger( Animal a ) {
return a.speak() + a.speak() + a.speak() + "!";
}
|
Note, incidentally, that we can call this method on either
a Cat,
or on
a Python.
This method doesn't know (and doesn't care),
what subclass of Animal it gets;
every Animal can speak, so
this method works just fine.
-
If a superclass has been declared as an abstract class,
it means you can't have objects which are the supertype without
actually being some specific subtype.
That is: you can't call their constructor directly.
-
constructor
-
You can have actual code and fields
in a superclass (unlike an interface).
-
We've actually already seen inheritance:
all classes implicitly extend Object.
-
We can add behavior specific to new classes;
for instance, perhaps Dogs can wagTail
(which Cats are simply not able to do).
(click for .jar)
-
Overriding.
We can make Dog's speak method different from
all other Animals.
Call polymorphically (exciting! (?)):
if we call communicateHunger,
that method still doesn't know or care what type of Animal it gets;
moreover, it doesn't even know exactly which method will be called!
(That's okay -- the animal itself knows where it's code for speak is.)
-
Note that we've seen overriding already:
we've overriden toString.
Similarly, equals just uses == by default,
but some classes (like String)
have overridden equals to do something more specific.
Don't confuse “overriding” with the
much shallower concept, “overloading”.
-
More inheritance and overriding.
(click for .jar)
-
(Advanced): What if you are overriding a method (say,
SmallDog's speak),
and in the process of doing a SmallDog's
speak you want to do a regular old speak?
If you just call speak, that's the same as this.speak,
which is the very method you're in the middle of writing;
you don't want to call yourself!
Solution: use super.speak()
(as opposed to this.speak()).
In Java, every non-static method has two variables which are automatically
provided for you: this (a reference to yourself)
and super (a reference to yourself, but in the guise
of your superclass).
Note that these two words are also abused a bit: they are also
the keywords used to call a constructor from within a constructor.
-
Caution:
In Java,
You can't override fields.
If you include a field name down in class Cat,
then polymorphic code (which is using a variable of
type Animal will use the field from inside Animal,
not the one from inside Cat!
(But code which knows its variable is declared type is
Cat will use the field inside
the subclass.)
Upshot: Don't override fields; it causes hard-to-find bugs
and most Java compilers don't give you any warnings.
1Every single
method we've written this semester could have been made static!
We would just have to make one change to each method: add
another parameter -- named this.
Then when calling the method, rather than saying jo.setSalary(12.00),
we'd just have called setSalary(jo, 12.00).
With this simple transformation, we could have done everything this semester
without ever needing methods inside of a class (though we'd still need fields inside objects).
↩
home—info—exams—lectures—labs—hws
syntax—Laws—lies—java.lang docs—java.util docs