26-polymorphism - CSE 143 Lecture 23 Polymorphism; the...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 143 Lecture 23 Polymorphism; the Object class read 9.2 - 9.3 slides created by Marty Stepp and Ethan Apter http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Polymorphism polymorphism : 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. A Scanner can read data from any kind of InputStream . Every kind of OutputStream can write data, though they might write this to different kinds of sources.
Background image of page 2
3 Inheritance in WoW Gear Chest Hands Helm Shoulders Pants ChainmailChest LeatherChest ClothChest WoolChest PlateChest RuneclothChest NetherweaveChest CopperPlateChest AdamantiteChest
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Coding with polymorphism A variable of type T can refer to an object of any subclass of . Chest chestpiece = new PlateChest(); Gear loot = new RuneclothChest(); You can call any methods from Chest on chestpiece . You can not call any methods specific to PlateChest (e.g. smelt ). When a method is called on chestpiece , it behaves as a PlateChest . System.out.println( chestpiece.getArmor() ); // 742 System.out.println( chestpiece.reqLevel() ); // 56
Background image of page 4
5 Polymorphism/parameters public class GearMain { public static void main(String[] args) { PlateChest plate = new PlateChest(); CopperChest copper = new CopperPlateChest(); printInfoPlate(plate); printInfoCopper(copper); } } public static void printInfoPlate( PlateChest loot ) { System.out.println(”armor = " + loot.getArmor()); System.out.println(”required level = " + loot.reqLevel()); System.out.println(”soulbinds = " + loot.canSoulBind()); System.out.println(); } public static void printInfoCopper( CopperPlateChest loot ) { System.out.println(”armor = " + loot.getArmor()); System.out.println(”required level = " + loot.reqLevel()); System.out.println(”soulbinds = " + loot.canSoulBind()); System.out.println(); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Polymorphism/parameters You can pass any subtype of a parameter's type. public class GearMain { public static void main(String[] args) { PlateChest plate = new PlateChest(); CopperChest copper = new CopperPlateChest(); printInfo(plate); printInfo(copper); } public static void printInfo( Gear loot ) { System.out.println(”armor = " + loot.getArmor()); System.out.println(”required level = " + loot.reqLevel()); System.out.println(”soulbinds = " + loot.canSoulBind()); System.out.println(); } } OUTPUT: armor = 100 armor = 110 required level = 10 required level = 14 soulbinds = false soulbinds = true
Background image of page 6
7 Coding with polymorphism We can use polymorphism with classes like OutputStream . Recall methods common to all OutputStream s: Recall part of the inheritance hierarchy for OutputStream : Method Description write (int b) writes a byte close () stops writing (also flushes) flush() forces any writes in buffers to be written OutputStream FileOutputStream PrintStream FilterOutputStream
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Streams and polymorphism A variable of type T can refer to an object of any subclass of T. OutputStream out
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/30/2011 for the course COMPUTER S 143 taught by Professor John during the Spring '11 term at Washington State Community College.

Page1 / 36

26-polymorphism - CSE 143 Lecture 23 Polymorphism; the...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online