public class Base {
protected int theInt = 100;
...
public void printTheInt() {
System.out.println( theInt );
}
}
public class Doubler extends Base {
...
public void printTheInt() {
System.out.println( theInt*2 );
}
}
public class Tripler extends Base {
...
public void printTheInt() {
System.out.println( theInt*3 );
}
}
public class Squarer extends Tripler {
...
public void printTheInt() {
System.out.println( theInt*theInt );
}
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.2.1
Some client code
Base theBase;
theBase = new Base();
theBase.printTheInt();
theBase = new Doubler();
theBase.printTheInt();
theBase = new Tripler();
theBase.printTheInt();
theBase = new Squarer();
theBase.printTheInt();
theBase = new Base();
theBase.printTheInt();
...
output
Base
subtype polymorphism
As dynamic binding occurs
the behavior (i.e., methods)
follow the objects.
Doubler
Tripler
Squarer
© 2006 Pearson Addison-Wesley. All rights reserved
9.2.2
Polymorphism comes from the words
“poly” ... many
“morph” ... to change
A polymorphic variable can appear to change its type through
dynamic binding.
The compiler always understands a
variable’s type according to its declaration.
The compiler permits some flexibility by way of type
conformance.
At run-time the behavior of a method call depends upon the
type of the object and not the variable.
theBase = new Squarer();
theBase.printTheInt();
© 2006 Pearson Addison-Wesley. All rights reserved
9.2.3
Another Example of Polymorphism
public class Sinker
extends Component {
...
public void doTheKitchenSink( int k ) {
}
} public class SlugWithSink
Sinker sink;
SlugWithSink slug;
BugWithSink bug;
RugWithSink rug;
extends Sinker {
...
public void doTheKitchenSink( int k ) {
setLocation( k, k );
}
}
public class BugWithSink
extends Sinker {
...
public void doTheKitchenSink( int k ) {
setSize( k, 10 );
}
}
public class RugWithSink
extends Sinker {
...
public void doTheKitchenSink( int k ) {
setLocation( 0, k );
}
}
slug = new SlugWithSink();
rug = new RugWithSink();
bug = new BugWithSink();
sink = slug;
sink.doTheKitchenSink(5);
sink = bug;
sink.doTheKitchenSink(10);
sink = rug;
sink.doTheKitchenSink(2);
sink = slug;
sink.doTheKitchenSink(3);
© 2006 Pearson Addison-Wesley. All rights reserved
What
if Sinker does NOT include doTheKitchenSink() ?
9.2.4
Why is Polymorphism Useful?
Polymorphism permits a superclass to capture commonality,
leaving the specifics to subclasses.
Suppose that JComponent included
an area method, as shown.
Then Rectangle could be
written as ...
and Oval could be
written as ...
public class JComponent {
...
public double area() {
return 0.0;
}
}
public class Rectangle extends JComponent {
...
public double area() {
return getWidth() * getHeight();
}
}
public class Oval extends JComponent {
...
public double area() {
return getWidth()/2. * getHeight()/2. * Math.PI;
}
}
now consider
public double coverageCost( JComponent v, double costPerSqUnit) {
return v.area() * costPerSqUnit;
© 2006 Pearson Addison-Wesley. All rights reserved
}
9.2.5
Object
Object
Every class inherits from a class called Object.
The println method is overloaded. One
version of println is defined as follows.
public void println( Object x ) {
println( x.toString() );
}
...
«query»
+ boolean equals( Object )
+ String toString()
...
Oval could override toString() in the following manner.
public String toString() {
String result = "Oval object" + "\n";
result = result + " x: " + getX() +", y: "+getY();
result = result + " width: " + getWidth() +", height: "+getHeight() + "\n";
if (getParent() == null) {
result = result + " not added, \n";
} else {
result = result + " is added, \n";
}
result = result + " color: "+colorString(getBackground());
return result;
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.2.6
Equality
There are two different kinds of equality for reference data.
Identity equality means that two expressions have the same identity.
(i.e., the two expressions represent the same object.)
Content equality means that two expressions represent objects
with the same value/content .
The == symbol tests for identity equality, when applied to reference data.
Example
Oval ov1, ov2;
ov1 = new Oval(0, 0, 100, 100);
ov2 = new Oval(0, 0, 100, 100);
if (ov1 == ov2) {
System.out.println( “identity equality” );
} else {
System.out.println( “not identity equality” );
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.2.7
Content Equality
The equals method from Object provides a potential content equality check.
Example
public class MyOval extends Oval {
...
public boolean equals(Object z) {
return (z instanceof Oval)
&& ((Oval)z).getX() == getX()
&& ((Oval)z).getY() == getY()
&& ((Oval)z).getWidth() == getWidth()
&& ((Oval)z).getHeight() == getHeight()
&& ((Oval)z).getBackground() == getBackground()
&& ((Oval)z).getParent() == getParent();
}
...
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.2.8
© Copyright 2025 Paperzz