Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces...

20
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9

description

Copyright 2009 by Pearson Education 3 Coding with polymorphism A variable of type T can hold an object of any subclass of T. Character rob = new Robot(); You can call any methods from Character on rob. You can not call any methods specific to Robot (e.g. sayQuote ). When a method is called on sam, it behaves as a Robot. System.out.println(sam.getAttack()); // 20 System.out.println(sam.toString()); // HP: 100

Transcript of Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces...

Page 1: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education

Building Java ProgramsChapter 9: Inheritance and Interfaces

Lecture 9-2: Polymorphismreading: 9.2

self-check: #5-9

Page 2: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 2

Polymorphismpolymorphism: Ability for the same code to be used with

different types of objects and behave differently with each.

System.out.println can print any type of object.Each one displays in its own way on the console.

CritterMain can interact with any type of critter.Each one moves, fights, etc. in its own way.

Page 3: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 3

Coding with polymorphismA variable of type T can hold an object of any subclass of T.

Character rob = new Robot();

You can call any methods from Character on rob.You can not call any methods specific to Robot (e.g. sayQuote).

When a method is called on sam, it behaves as a Robot.

System.out.println(sam.getAttack()); // 20System.out.println(sam.toString()); // HP: 100

Page 4: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 4

Polymorphism + parametersMethods can accept superclass types as parameters.

You can pass any subtype of that superclass.public class TestCharacters { public static void main(String[] args) { KnightWhoSaysNi knight = new KnightWhoSaysNi(); Robot robRobot = new Robot(); basicAbilities(knight); basicAbilities(robRobot); } // prints the result of some of the methods in a Character public static void basicAbilities(Character player) { System.out.println("toString: " + player); System.out.println("getAttack: " + player.getAttack()); System.out.println("isAlive: " + player.isAlive()); System.out.println(); }} OUTPUT:toString: HP: 100getAttack: 10isAlive: true

toString: HP: 100getAttack: 20isAlive: true

Page 5: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 5

Polymorphism + arraysArrays of superclass types can store any subtype as elements.

public class TestCharacters2 { public static void main(String[] args) { Character[] c = { new KnightWhoSaysNi(), new Robot(), new Sorcerer(), new Oracle() }; for (int i = 0; i < e.length; i++) { System.out.println("attack: " + c[i].getAttack()); System.out.println("toString: " + c[i].toString()); System.out.println(); } }}

Page 6: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 6

Polymorphism problems~4-5 classes with inheritance relationships are shown.

A client program calls methods on objects of each class.

You must read the code and determine the client's output.

Page 7: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 7

A polymorphism problemAssume that the following four classes have been declared:

public class Foo { public void method1() { System.out.println("foo 1"); } public void method2() { System.out.println("foo 2"); } public String toString() { return "foo"; }}public class Bar extends Foo { public void method2() { System.out.println("bar 2"); }}

Page 8: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 8

A polymorphism problempublic class Baz extends Foo { public void method1() { System.out.println("baz 1"); } public String toString() { return "baz"; }}public class Mumble extends Baz { public void method2() { System.out.println("mumble 2"); }}

What would be the output of the following client code?Foo[] f = {new Baz(), new Bar(), new Mumble(), new Foo()};for (int i = 0; i < f.length; i++) { System.out.println(f[i]); f[i].method1(); f[i].method2(); System.out.println();}

Page 9: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 9

Diagramming the classesAdd classes from top (superclass) to bottom (subclass).Include all inherited methods.

Page 10: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 10

Finding output with tables

method Foo Bar Baz Mumble

method1

method2

toString

method Foo Bar Baz Mumble

method1 foo 1 baz 1

method2 foo 2 bar 2 mumble 2

toString foo baz

method Foo Bar Baz Mumble

method1 foo 1 foo 1 baz 1 baz 1

method2 foo 2 bar 2 foo 2 mumble 2

toString foo foo baz baz

Page 11: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 11

Polymorphism answerFoo[] f = {new Baz(), new Bar(), new Mumble(), new Foo()};for (int i = 0; i < f.length; i++) { System.out.println(f[i]); f[i].method1(); f[i].method2(); System.out.println();}Output:

bazbaz 1foo 2foofoo 1bar 2bazbaz 1mumble 2foofoo 1foo 2

Page 12: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 12

Another problemThe order of the classes is jumbled up.The methods sometimes call other methods (tricky!).

public class Lamb extends Ham { public void b() { System.out.print("Lamb b "); }}public class Ham { public void a() { System.out.print("Ham a "); b(); } public void b() { System.out.print("Ham b "); } public String toString() { return "Ham"; }}

Page 13: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 13

Another problem 2public class Spam extends Yam { public void b() { System.out.print("Spam b "); }}public class Yam extends Lamb { public void a() { System.out.print("Yam a "); super.a(); } public String toString() { return "Yam"; }}

What would be the output of the following client code?Ham[] food = {new Lamb(), new Ham(), new Spam(), new Yam()};for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); System.out.println(); // to end the line of output food[i].b(); System.out.println(); // to end the line of output System.out.println();}

Page 14: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 14

Class diagram

Page 15: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 15

Polymorphism at workLamb inherits Ham's a. a calls b. But Lamb overrides b...public class Ham { public void a() { System.out.print("Ham a "); b(); } public void b() { System.out.print("Ham b "); } public String toString() { return "Ham"; }}public class Lamb extends Ham { public void b() { System.out.print("Lamb b "); }}

Lamb's output from a:Ham a Lamb b

Page 16: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 16

The table

method Ham Lamb Yam Spam

a

b

toString

Page 17: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 17

The answerHam[] food = {new Lamb(), new Ham(), new Spam(), new Yam()};for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); food[i].b(); System.out.println();}Output:

HamHam a Lamb bLamb bHamHam a Ham bHam bYamYam a Ham a Spam bSpam bYamYam a Ham a Lamb bLamb b

Page 18: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 18

Inheritance Mystery Summary Draw the inheritance hierarchy

Create the chart Start from the top of the hierarchy downward When you see super.something(), copy the contents of

the superclasses’s something() method into the chart When you see a (non-super) method call, copy the method call

into the chart Not the contents of the method call, since this may change

depending on inheritance!

Use the chart to write the final output

Page 19: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education

Building Java ProgramsParameters and References Review

Page 20: Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Copyright 2009 by Pearson Education 20

Parameters - value semanticsRecall:

For primitives, parameters are initialized by copying the valueFor objects, parameters are initialized by copying the reference

An “arrow” to the object is copied (not the array or object itself).

Consequence: If you change the primitive variable inside the called method, it

has no effect on the variable in the scope of the callerIf you change the array or object in the called method, it's the

same array or object that the caller has, so the caller’s objects change too!

More practice in section