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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP T1.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Java Syntax Primitive data types Operators Control statements.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Interfaces and Polymorphism CS 162 (Summer 2009).
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
3 Introduction to Classes and Objects.
“Form Ever Follows Function” Louis Henri Sullivan
Software Development Java Classes and Methods
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Java Programming Language
Java Programming Language
Presentation transcript:

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

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

© Peter Andreae COMP :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

© Peter Andreae COMP :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

© Peter Andreae COMP :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

© Peter Andreae COMP :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()

© Peter Andreae COMP :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

© Peter Andreae COMP :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){….. }

© Peter Andreae COMP :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

© Peter Andreae COMP :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

© Peter Andreae COMP :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!

© Peter Andreae COMP :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();

© Peter Andreae COMP :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.

© Peter Andreae COMP :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"

© Peter Andreae COMP :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 { …. }

© Peter Andreae COMP :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.

© Peter Andreae COMP :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.

© Peter Andreae COMP :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) { …… } }

© Peter Andreae COMP :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.

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

© Peter Andreae COMP :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.

© Peter Andreae COMP :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( …

© Peter Andreae COMP :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)

© Peter Andreae COMP :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( …