Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP."— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP 102 #29 2014

2 © Peter Andreae COMP 102 29:2 Outline Multiple types Interface classes Implementing Interface classes. Reading: Textbook 9.3: Interfaces (note, textbook also discusses inheritance)

3 © Peter Andreae COMP 102 29:3 Types again Variables, fields, array elements are all defined with a type Only values of the specified type can be put into a place What types are there primitive types: int, double, boolean, long, float, char,.. note: java will "coerce" some types into other types: double number = 4; Object types: Arrays : int[ ], double[ ][ ], Disk[ ], Predefined: String, Color, File, Scanner, … Programmer defined: CartoonFigure, Disk, DiskGame, … Type of object determined at its creation: new Disk(100, 300) ⇒ a Disk object new File ("data.txt") ⇒ a File object new int[100][100]⇒ an array of arrays of int. "Hello"⇒ a String Every class defines a type

4 © Peter Andreae COMP 102 29:4 Object types All object values have two types: the type they were created as the Object type You can have a place of type Object: Object value = new Disk(100, 100); value = "not any more"; There are limits on what you can do with a place of type Object: value.explode(); value.toUpperCase(); You can only call methods on the value in a variable/field if the methods belong to the type of the variable/field. value.toString() Won't work if value contains a String Won't work if value contains a Disk

5 © Peter Andreae COMP 102 29:5 Collections of different types Extending the Disk Game: Have several kinds of Disks: ordinary Disks – get damaged, and explode after several shots bomb Disks – blow up immediately taking out lots of disks fat disks – get smaller as get damaged, and don’t explode bouncy disks – don't explode, but bounce around when shot T T X

6 © Peter Andreae COMP 102 29:6 New Disk Game DiskGame needs an array to hold all the disks Each class of disks needs the same methods: damage, on, explode, etc but, they each behave differently. DiskGame array of disks RoundDisk damage() on(int x, int y) explode() BombDisk damage() on(int x, int y) explode() FatDisk damage() on(int x, int y) explode() BouncyDisk damage() on(int x, int y) explode()

7 © Peter Andreae COMP 102 29:7 DiskGame public class DiskGame { : private int maxDisks = 20; private [ ] disks = new [maxDisks]; public void hitDisk(int x, int y){ for (int i = 0; i<disks.length; i++){ if (disks[i] != null && this.disks[i].on(x, y) ) { this.disks[i].damage(); : RoundDisk-2BombDisk-5 FatDisk-8 BouncyDisk-3 RoundDisk-7 Java will complain! No such method for Object Java compiler will complain! No such method for Object Can use Object Object RoundDisk-3 FatDisk-12

8 © Peter Andreae COMP 102 29:8 Different Disk classes public class RoundDisk { private int x, y; private int radius = 10; private Color col; public void damage(int amt){….. public boolean on(int x, int y){….. } public class FatDisk { private int x, y; private int length = 10; private double direction = 1.26; private Color baseCol, tubeCol; public void damage(int amt){….. public boolean on(int x, int y){….. }

9 © Peter Andreae COMP 102 29:9 "Generic" values with Object public class DiskGame { : private int maxDisks = 20; private Object [ ] disks = new Object [maxDisks]; public void hitDisk(int x, int y){ for (int i = 0; i<disks.length; i++){ Object disk = this.disks[i]; RoundDisk rd = (RoundDisk) disk; if (rd != null && rd.on(x, y) ) { rd.damage(); Cast it to a RoundDisk Now compiler is happy But if it is a BombDisk or … the program will fall over! RoundDisk-2BombDisk-5 FatDisk-8 BouncyDisk-3 RoundDisk-7 RoundDisk-3 FatDisk-12

10 © Peter Andreae COMP 102 29:10 Casting Can sometimes convert a value from one type to another numbers: int col = (int) (x /cellWidth); double x = 100 * col; Must be an explicit cast if information might be lost. anything to a string: (by adding to a string) "value is " + x For an Object value, it automatically calls the toString() method "value is " + x.toString() any object value from one of its types to another Object disk = (Object) ( new RoundDisk(34, 45) ); RoundDisk rd = (RoundDisk) disk; object must be of that type, otherwise a runtime error

11 © Peter Andreae COMP 102 29:11 "Generic" values with Object public void hitDisk(int x, int y){ for (int i = 0; i<disks.length; i++){ Object disk = this.disks[i]; if (disk == null) { continue; } else if (disk instanceof RoundDisk) { RoundDisk rd = (RoundDisk) disk; if (rd.on(x, y) ) { rd.damage();…..} } else if (disk instanceof FatDisk) { FatDisk fd = (FatDisk) disk; if (fd.on(x, y) ) { fd.damage();…..} } … RoundDisk-2BombDisk-5 FatDisk-8 BouncyDisk-3 RoundDisk-7 null Not nice code!

12 © Peter Andreae COMP 102 29:12 A Disk type RoundDisk, FatDisk, BombDisk, BouncyDisk are all kinds of Disk. ⇒ should all be of a Disk type. RoundDisk-2 should be a a RoundDisk, a Disk, and an Object public class DiskGame { private int maxDisks = 20; private Disk [ ] disks = new Disk [maxDisks]; public void hitDisk (int x, int y){ for (int i = 0; i<disks.length; i++){ Disk disk = this.disks[i]; if (disk != null && disk.on(x, y) ) { disk.damage();

13 © Peter Andreae COMP 102 29:13 Making a Disk Type Problem: What is "a Disk" ? Answer 1: Something you can damage, public void damage() ask if a point is on it,public boolean on(int x, int y) explodepublic void explode() ask for its scorepublic double score() ask if within range of disk public boolean withinRange(Disk other) Answer 2: a RoundDisk, or a FatDisk, or a BombDisk, or a BouncyDisk. We need to make both answers true.

14 © Peter Andreae COMP 102 29:14 Interface classes An interface class describes a supertype of other types. It specifies a name for the type the headers for the methods that any object of the type can respond to. constants (public static final double GRAVITY = 9.8) nothing else! (no fields, constructors, or method bodies) public interface Disk { method headers constants } You cannot make a new instance of an interface. Disk b = new Disk(); An interface just says "what"; not "how"

15 © Peter Andreae COMP 102 29:15 A Disk Interface class. public interface Disk { public void damage(); public boolean on(int x, int y); public void explode(); public double score(); public boolean withinRange(Disk other); } Declares a type that you can use for fields,variables, arrays: public class DiskGame { private int maxDisks = 20; private Disk [ ] disks = new Disk [maxDisks]; public void hitDisk(int x, int y){ for (int i = 0; i<disks.length; i++){ Disk disk = this.disks[i]; if (disk != null && disk.on(x, y) ) { disk.damage(); Java will NOT complain! Disks DO have these methods Note the ; instead of { …. }

16 © Peter Andreae COMP 102 29:16 Making a Disk Type Problem: What is "a Disk" ? Answer 1: Defined by interface Disk { Something you can damage, ask if a point is on it, explode ask for its score ask if within range of another disk Answer 2: a RoundDisk, or a FatDisk, or a BombDisk, or a BouncyDisk. We need to make both answers true.

17 © Peter Andreae COMP 102 29:17 Making classes have another type If you define some class, all instances are automatically of type Object also. To make instances be of some interface type: declare that the class implements the interface type: make sure that the class defines all the methods specified by the interface type: public class RoundDisk implements Disk { private int x, y; private int radius = 10; public void damage(int amt){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } } Must provide method bodies, not just headers! implements is a “ promise ” that these objects will be of the interface type. ie, will have all the methods.

18 © Peter Andreae COMP 102 29:18 More classes implementing Disk public class FatDisk implements Disk { private int x, y; private int layers = 5; public void damage( ){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } } public class BombDisk implements Disk { private int x, y, maxRad; private int radius = 10; public void damage(){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } }

19 © Peter Andreae COMP 102 29:19 Making a Disk Type Problem: What is "a Disk" ? Answer 1: Defined by interface Disk { Something you can damage, ask if a point is on it, explode ask for its score ask if within range of another disk Answer 2: Specified by … implements Disk a RoundDisk, or a FatDisk, or a BombDisk, or a BouncyDisk. We have now made both answers true.

20 © Peter Andreae COMP 102 29:20 DiskGame class structure RoundDisk damage explode … Disk FatDisk damage explode … BombDisk damage explode … BouncyDisk damage explode … DiskGame User Interface Array of Disk

21 © Peter Andreae COMP 102 29:21 Interface class summary If you define an interface class: You have defined a type You can declare variables (or arrays) to have that type. You cannot make an object of the interface class new Disk(…) // NOT ALLOWED (there is no constructor) Objects from classes implementing the interface are of that type. You can call any of the specified methods on values in fields/variables of that type. When defining an interface class: You should include headers of methods You may include static final constants You may not include fields, constructors, or method bodies.

22 © Peter Andreae COMP 102 29:22 Interfaces: More examples We have used interfaces before! public class Plotter implements UIButtonListener{ public Plotter(){ UI.addButton(“Read”, this); UI.addButton(“Plot”, this); UI.addButton(“Stats”, this); } Read Plot Stats Button-7 name: aListener: : “ Read ” Button-11 name: aListener: : “ Plot ” Button-8 name: aListener: : “ Stats ” Plotter-54 numbers: count: public void actionPerformed( …

23 © Peter Andreae COMP 102 29:23 UIButtonListener is an Interface UIButtonListener is an Interface class Every button object contains a field of type UIButtonListener UIButtonListener specifies one method: public interface UIButtonListener{ public void buttonPerformed(String button); } When a button is pressed. the JVM looks for the object in the button’s buttonListener field It then calls the buttonPerformed method on the object passing the name of the button. It can only do this because the object is an UIButtonListener. (and this is almost the only thing it can do on the object)

24 © Peter Andreae COMP 102 29:24 Interfaces: More examples The listener can be a Plotter, or a Genealogy, or a DiskGame … as long as it is also an UIButtonListener DoB Parents Children Button-18 name: aListener: : “ DoB ” Button-21 name: aListener: : “ Parents ” Button-5 name: aListener: : “ Children ” Genealogy-2 persons: public void actionPerformed( …


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP."

Similar presentations


Ads by Google