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

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
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.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Topic 3 The Stack ADT.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
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 Arrays with meaningful indices.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Types in programming languages1 What are types, and 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 Classes, Objects, Fields,
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 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 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.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
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.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
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. 
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
3 Introduction to Classes and Objects.
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
Introduction to Java Programming
Java Programming Language
Classes CS 21a: Introduction to Computing I
Subtype Substitution Principle
Presentation transcript:

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

© Peter Andreae COMP :2 Outline Multiple types Interface classes Implementing Interface classes. Admin: Last assignment on Wednesday Tutorial: Tues 4-5

© Peter Andreae COMP :3 BalloonGame Simple game Collection of Balloons Clicking on a Balloon expands it If two Balloons touch, they pop. Score = total area of live balloons minus area of popped Balloons. Key data structure: Like DominoGame and Genealogy: needs a list of Balloon objects: ArrayList

© Peter Andreae COMP :4 BalloonGame

© Peter Andreae COMP :5 Design of BalloonGame

© Peter Andreae COMP :6 Types and Interface 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[ ][ ], Balloon[ ], Predefined: String, Color, File, Scanner, … Programmer defined: CartoonFigure, Balloon, BalloonGame, … Type of object determined at its creation: new Balloon(100, 300) ⇒ a Balloon 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 :7 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 Balloon(100, 100); value = "not any more"; ArrayList things = new ArrayList (); things.add("more", new Balloon(100, 100), new Double(4.5)); for (Object thing : things){ UI.println(thing.toString()); System.out.println(thing); }

© Peter Andreae COMP :8 Object types There are limits on what you can do with a place of type Object: value.pop(); value.toUpperCase(); for (Object thing : things){ if (thing.startsWith(“Ball”){ System.out.println(thing); } } 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 Balloon

© Peter Andreae COMP :9 Collections of different types Extending the Balloon Game: several kinds of balloons: ordinary balloons – expand uniformly, and pop on touching bomb balloons – blow up, taking out lots of balloons at max size long balloons – expand into sausage shapes, popping other balloons only, up to limit bouncy balloons – don't expand, but bounce around and move other balloons T

© Peter Andreae COMP :10 New Balloon Game BalloonGame needs an array to hold all the balloons Each class of balloons needs the same methods: expand, on, pop but, they each behave differently. BalloonGame array of balloons RoundBalloon expand() on(int x, int y) pop() BombBalloon expand() on(int x, int y) pop() LongBalloon expand() on(int x, int y) pop() BouncyBalloon expand() on(int x, int y) pop()

© Peter Andreae COMP :11 BalloonGame public class BalloonGame { : private int maxBalloons = 20; private [ ] balloons = new [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ if (balloons[i] != null && this.balloons[i].on(x, y) ) { this.balloons[i].expand(); : RoundBalloon-2BombBalloon-5 LongBalloon-8 BouncyBalloon-3 RoundBalloon-7 null Java will complain! No such method for Object Java compiler will complain! No such method for Object Can use Object Object

© Peter Andreae COMP :12 Different Balloon classes public class RoundBalloon { private int x, y; private int radius = 10; private Color col; public void expand(int amt){….. public boolean on(int x, int y){….. } public class LongBalloon { private int x, y; private int length = 10; private double direction = 1.26; private Color baseCol, tubeCol; public void expand(int amt){….. public boolean on(int x, int y){….. }

© Peter Andreae COMP :13 "Generic" values with Object public class BalloonGame { : private int maxBalloons = 20; private Object [ ] balloons = new Object [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Object b = this.balloons[i]; RoundBalloon rb = (RoundBalloon) b; if (rb != null && rb.on(x, y) ) { rb.expand(); RoundBalloon-2BombBalloon-5 LongBalloon-8 BouncyBalloon-3 RoundBalloon-7 null Cast it to a RoundBalloon Now compiler is happy But if it is a BombBalloon or … the program will fall over!

© Peter Andreae COMP :14 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 balloon = (Object) ( new RoundBalloon(34, 45) ); RoundBalloon rb = (RoundBalloon) balloon; object must be of that type, otherwise a runtime error

© Peter Andreae COMP :15 "Generic" values with Object public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Object b = this.balloons[i]; if (b == null) { continue; } else if (b instanceof RoundBalloon) { RoundBalloon rb = (RoundBalloon) b; if (rb.on(x, y) ) { rb.expand();…..} } else if (b instanceof LongBalloon) { LongBalloon lb = (LongBalloon) b; if (lb.on(x, y) ) { lb.expand();…..} } … RoundBalloon-2BombBalloon-5 LongBalloon-8 BouncyBalloon-3 RoundBalloon-7 null Not nice code!

© Peter Andreae COMP :16 A Balloon type RoundBalloon, LongBalloon, BombBalloon, BouncyBalloon are all kinds of Balloon. ⇒ should all be of a Balloon type. RoundBalloon-2 should be a a RoundBalloon, a Balloon, and an Object public class BalloonGame { private int maxBalloons = 20; private Balloon [ ] balloons = new Balloon [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Balloon b = this.balloons[i]; if (b != null && b.on(x, y) ) { b.expand();

© Peter Andreae COMP :17 Making a Balloon Type Problem: What is "a Balloon" ? Answer 1: Something you can expand, public void expand() ask if a point is on it,public boolean on(int x, int y) poppublic void pop() ask for its sizepublic double size() ask if touching a balloon public boolean touches(Balloon other) Answer 2: a RoundBalloon, or a LongBalloon, or a BombBalloon, or a BouncyBalloon. We need to make both answers true.

© Peter Andreae COMP :18 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 Balloon { method headers constants } You cannot make a new instance of an interface. Balloon b = new Balloon(); An interface just says "what"; not "how" Latest version of Java has extensions

© Peter Andreae COMP :19 A Balloon Interface class. public interface Balloon { public void expand(); public boolean on(int x, int y); public void pop(); public double size(); public boolean touches(Balloon other); } Declares a type that you can use for fields,variables, arrays: public class BalloonGame { private int maxBalloons = 20; private Balloon [ ] balloons = new Balloon [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Balloon b = this.balloons[i]; if (b != null && b.on(x, y) ) { b.expand(); Java will NOT complain! Balloons DO have these methods Note the ; instead of { …. }

© Peter Andreae COMP :20 Making a Balloon Type Problem: What is "a Balloon" ? Answer 1: Defined by interface Balloon { Something you can expand, ask if a point is on it, pop ask for its size ask if touching a balloon Answer 2: a RoundBalloon, or a LongBalloon, or a BombBalloon, or a BouncyBalloon. We need to make both answers true.

© Peter Andreae COMP :21 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 RoundBalloon implements Balloon { private int x, y;private int radius = 10; private Color col; public void expand(int amt){ …… } public boolean on(int x, int y){ …… } public void pop() { …… } public double size() { …… } public boolean touches(Balloon 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 :22 More classes implementing Balloon public class LongBalloon implements Balloon { private int x, y;private int length = 10; private double dir = 1.26; private Color baseCol, tubeCol; public void expand(int amt){ …… } public boolean on(int x, int y){ …… } public void pop() { …… } public double size() { …… } public boolean touches(Balloon other) { …… } } public class BombBalloon implements Balloon { private int x, y, maxRad;private int radius = 10; private Color col; public void expand(int amt){ …… } public boolean on(int x, int y){ …… } public void pop() { …… } public double size() { …… } public boolean touches(Balloon other) { …… } }

© Peter Andreae COMP :23 Making a Balloon Type Problem: What is "a Balloon" ? Answer 1: Defined by interface Balloon { Something you can expand, ask if a point is on it, pop ask for its size ask if touching a balloon Answer 2: Specified by … implements Balloon a RoundBalloon, or a LongBalloon, or a BombBalloon, or a BouncyBalloon. We have now made both answers true.

© Peter Andreae COMP :24 BalloonGame class structure RoundBalloon expand pop … Balloon LongBalloon expand pop … BombBalloon expand pop … BouncyBalloon expand pop … BalloonGame User Interface Array of Balloon

© Peter Andreae COMP :25 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 Balloon(…) // 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.