Download presentation
Presentation is loading. Please wait.
1
public class Doubler extends Base {
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
2
Some client code output subtype polymorphism As dynamic binding occurs
Base theBase; theBase = new Base(); theBase.printTheInt(); theBase = new Doubler(); theBase = new Tripler(); theBase = new Squarer(); ... Squarer Doubler Tripler Base subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. © 2006 Pearson Addison-Wesley. All rights reserved
3
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
4
Another Example of Polymorphism
public class Sinker extends Component { ... public void doTheKitchenSink( int k ) { } Sinker sink; SlugWithSink slug; BugWithSink bug; RugWithSink rug; 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.doTheKitchenSink(3); public class SlugWithSink 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 ); } © 2006 Pearson Addison-Wesley. All rights reserved What if Sinker does NOT include doTheKitchenSink() ?
5
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. public class JComponent { ... public double area() { return 0.0; } Then Rectangle could be written as ... public class Rectangle extends JComponent { ... public double area() { return getWidth() * getHeight(); } and Oval could be written as ... 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
6
Object Every class inherits from a class called Object.
. . . «query» + boolean equals( Object ) + String toString() 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() ); } 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
7
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
8
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
Arrays are like Objects
A whole (aggregate) array can be ... assigned to another array variable passed as a parameter private void colorAllRed( Rectangle[] rects ) { int ndx = 0; while ( ndx != rects.length ) { rects[ndx].setBackground( Color.red ); ndx++; } Rectangle[] someRects, otherRects; someRects = new Rectangle[3]; someRects[0] = new Rectangle(0,0,5,5); someRects[1] = new Rectangle(6,6,8,8); someRects[2] = new Rectangle(10,10,10,10); otherRects = someRects; colorAllRed( otherRects ); © 2006 Pearson Addison-Wesley. All rights reserved
10
Arrays are like Objects 2
Every array conforms to Object ... int[] myArray; myArray = (int[])makeAnObject(); private Object makeAnObject( ) { int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; processObject( daysInMonth ); return daysInMonth; } private void processObject( Object stuff ) { © 2006 Pearson Addison-Wesley. All rights reserved
11
Arrays are unlike Objects
There is no way to override Object methods. toString equals Inheritance of arrays is NOT permitted. public class myClass extends someClass[] © 2006 Pearson Addison-Wesley. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.