Inheritance
(notes for 10/26 lecture)
Inheritance
• Inheritance is the last of the relationships
we will study this semester.
• Inheritance is (syntactically) an easy
relationship to express, but it takes some
study to truly understand its many
aspects.
Flavors of inheritance
• Inheritance used for different purposes:
– to indicate conceptual relatedness (is-a)
– for subtyping (extension of capabilities)
– for code sharing
• Inheritance can take place from
– interface to interface
– class to class
Inheritance: is-a
• The is-a relationship exists between two
classes A and B if each member of A is
also a B.
• Example 1: since every square is also a
rectangle, we say square is-a rectangle.
• Example 2: since every penguin is also a
bird, we say penguin is-a bird.
• The picture on the next slide, shows this
idea.
shape Exhibit ‘A’
polygon
parallelogram
ellipse
rectangle rhombus
square
(conceptual) is-a hierarchy
Inheritance: types
• Inheritance can also indicate a subtype-
supertype relationship.
• A subtype has at least as many
capabilities as the supertype has.
• The compiler uses typing information to
ensure that method calls made on
expressions of a given type are coherent.
types (continued)
• It is possible to imagine that a rectangle object
in a drawing program allows its width and
height to be varied independently of each
other, via independent setWidth and setHeight
mutators.
• A square object must not allow this. A square
object in such an environment should only
provide a setSize mutator.
• From this perspective, squares and rectangles
are NOT of the same type.
• A rectangle and an ellipse might be, however.
Rectangles & Ellipses
width width
(x,y) (x,y)
(w) (w)
height (h) height (h)
(x+w,y+h) (x+w,y+h)
The dimensions of rectangles and ellipses are specified in the same manner.
types (continued)
• While a square is-a rectangle
conceptually, in this environment it does
not make sense for the type square be
be a subtype of the type rectangle (since
the set of methods on a rectangle are not
a subset of those of the square).
Exhibit ‘B’, part 1
shape
polygon
ellipse
rhombus
rectangle square
parallelogram
type hierarchy
Exhibit ‘B’, part 2
shape
rectangularly-defined
shape
ellipse rectangle square
type hierarchy
lesson
• There is no single “right” hierarchy.
• Arrange hierarchy so it works for your
problem/solution.
Inheritance:
sharing implemenation
• A third use of inheritance is for sharing of
implementation (code).
• This use of inheritance is over-used:
composition is often preferable.
• Advice:
– never use inheritance SOLELY for the
purpose of sharing implementation
– OK if coupled with is-a or typing motivation
Exhibit ‘C’ shape
square
rectangle rhombus polygon
ellipse parallelogram
(one way to achieve) code sharing
interface inheritance
• In Java, an interface can extend zero or
more interfaces:
• extending zero interfaces
public interface A { public void foo(); }
• extending one interface
public interface B extends A { public void bar(A a); }
• extending many interfaces
public interface C extends B, ActionListener {
public ActionListener getListener();
}
interface inheritance, cont’d
• When an interface A extends another interface
B, A inherits the methods specified by B.
• This means that a class which implements A
must define all the methods specified in both A
and B.
• If an interface can have at most one
specification for any given method: even it an
interface inherits the very same method
specification (same name, same parameter
list) from two or more parent interfaces, the
interface has the method specified just once.
class inheritance
• A (user-defined) class always extends
exactly one (other) class:
public class Circle extends Shape {…}
• If class header has no explicit extends
clause, parent class is implicitly Object:
public class Shape {…}
public class Shape extends Object {…}
• Object is the root of Java’s class hierarchy.
What is inherited?
• Given what we know, a correct answer is
that anything that is not private is
inherited.
• All our properties (instance variables) are
private, so they are not inherited.
• All our methods are public (not private),
so they are inherited.
What is effect of inheritance?
• A method inherited from a superclass to a
subclass can be invoked on a subclass
instance, even though not defined there:
public class Foo {
private Bar _bar;
public void setBar(Bar b) {
_bar = b;
}
}
public class FooSub extends Foo {
…
}
• This is legal:
new FooSub().setBar(new Bar())
Method overriding
• A subclass can override a definition inherited from
superclass simply by providing an alternate definition
of the method in the subclass’ defintion:
public class Foo {
private Bar _bar;
public void setBar(Bar b) {
_bar = b;
}
}
public class FooSub extends Foo {
public void setBar(Bar b) {
_bar = b; // of course, we can’t do this, since _bar is private!
b.setColor(new graphics.colors.Cyan());
}
}
partial overriding
• A subclass can add something to a definition inherited
from superclass simply first invoking the superclass’
definition, then adding extra code in an augmenting
definition of the method in the subclass’ defintion:
public class Foo {
private Bar _bar;
public void setBar(Bar b) {
_bar = b;
}
}
public class FooSub extends Foo {
public void setBar(Bar b) {
super.setBar(b); // asks superclass method to do: _bar = b;
b.setColor(new graphics.colors.Cyan());
}
}
overriding summary
• total (complete) overriding
– a subclass provides an entirely new definition for a
method which would otherwise have been inherited
from the superclass
• partial overriding
– a subclass provides a definition for a method which
would otherwise have been inherited from the
superclass, but calls the superclass version via
super.
• inheritance
– a subclass does not provide an alternate defintion
for a method defined in the superclass, which is
inherited.
A few loose ends…
• The next several slides ties up a few
loose ends.
constructor and method
overloading
• A class can define multiple methods with the
same name, as long as they differ in their
parameter lists.
• A class can therefore define multiple
constructors (which all MUST share their
name), as long as they differ in their parameter
lists.
• Providing multiple method definitions with the
same name is called overloading: the name is
overloaded with multiple definitions. Selection
of correct definition is based on the argument
list in a given method call.
overloading vs. overriding
• overloading:
– same name is used for many different method
definitions
– parameter lists of methods must all be different
– all definitions co-exist
• overriding:
– same name is used for a subclass method also
defined in a superclass
– parameter list must be exactly the same in subclass
definition as in superclass definition
– subclass definition supplants superclass definition
constructor overriding
• Constructors can (and should generally) be
overridden when extended classes other than
Object.
• A superclass constructor is always called,
implicitly if not explicitly. Implicit call is to
super().
• Use super with argument list to explicitly call
one of the superclass’ other constructors.
• Call to superclass constructor is ALWAYS the
first statement. If not, compiler complains.
no constructor?
• A class must always provide a
constructor.
• If not, one is provided implicitly: a no-
argument constructor which just calls the
implicit superclass constructor.
a minimial class definition
• In Java we can write:
public class Foo {
}
• In reality the compiler sees this:
public class Foo extends Object {
public Foo() {
super();
}
}
this
• A pronoun that an object uses to refer to
itself.
• A reference to the object on which a
method was invoked.
java.lang.System.out.println
method
• We can output (print) a String by using the method
System.out.println
• System is a class, in the package java.lang, all of
whose members are automagically imported (i.e. we
don’t need to use their fully-qualified names to access
them).
• out is a variable accessible via this class name (hmm
– how does that work?)
• println is a method defined on the object referred to by
the out variable.
• out is of type java.io.PrintStream, and overloads println
in many ways.