University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar 26 2010.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Interfaces A Java interface is a collection
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism Lecture 20, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Inheritance II Lecture 34, Mon Apr
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Inheritance II Lecture 23, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Mathematical Operations, Static Methods Lecture 9, Thu Feb 2.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Inheritance Lecture 22, Tue Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Parameter/Scope Review II, Inheritance Lecture 33,
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Class Design Lecture 9, Mon Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 11, Fri Jan
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Mathematical Operators, Static Methods Lecture 14,
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays II Lecture 22, Wed Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Terms and Rules Professor Evan Korth New York University (All rights reserved)
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 12, Mon Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design II Lecture 31, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Objects, Class Design Lecture 8, Fri Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 28, Wed Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
Abstract Classes and Interfaces
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Classes - Intermediate
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Topic: Classes and Objects
Interfaces and Polymorphism
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.
Polymorphism.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar borrowing from slides by Kurt Eiselt and Paul Carter

2 Midterm 2 n raw scores average: 27/60 n percentage scores scaled average: 66/100 n exams back, solutions not distributed until after correction lab in week 11

3 News: Reminder n change for labs n week 11 was no lab. now will be optional midterm review/correction n a chance to work through your mistakes and get some marks back n people with Monday (holiday) labs or conflicts can attend another lab and/or work on their own. anyone bring in corrected midterm at beginning of the week 12 lab if not finished working through during week 11 lab

4 Reading n Weeklies due today for this week n Next week: n (3rd ed) n (2nd ed)

5 Recap: Method Overloading n Can have multiple methods of same name n Distinguishes between them with signature n method name, parameter types and order n Cannot have two methods with same signature n Return type is not part of signature n Any method can be overloaded n constructors are very common case

6 Recap: Interfaces n Interface is collection of constants and abstract methods n different meaning than set of public methods that are documented, as in API n to implement interface must provide definitions for all its methods n Abstract methods have no implementation or body n method header followed by semicolon n specifies how to communicate with method, not what it does

7 Recap: Interface Example public interface VendingMachine { public void vendItem(); public int getItemsRemaining(); public int getItemsSold(); public double getCashReceived(); public void loadItems(int n); } public class CokeMachine2005 implements VendingMachine {

8 Recap: Interface Syntax n Use reserved word interface instead of class in header n no need to use reserved word abstract in method headers, is automatic with interfaces n Use reserved word implements followed by interface name in class header

9 Recap: Polymorphism n Polymorphism: behavior varies depending on actual type of object n variables can be declared with interface as type, can invoke interface methods on them n cannot construct interface n can only construct objects of some particular class that implements interface n Polymorphism determined at runtime n vs. method overloading, determined at compilation

10 Recap: Polymorphism Example public class SimCoke2005 { public static void main (String[] args) { VendingMachine foo1 = new CokeMachine2005(); VendingMachine foo2 = new FrenchFryMachine2005(); foo1.vendItem(); foo2.vendItem(); } Adding another CokeMachine to your empire Adding another FrenchFryMachine to your empire Have a Coke 9 cans remaining Have a nice hot cup of french fries 9 cups of french fries remaining

11 Polymorphism n reference to interface type can reference instance of any class implementing that interface n static type: type that variable declared to be n determines which members of class can be invoked n dynamic type: type that variable actually references n determines which version of method is called

12 Interfaces as Contract n Can write code that works on anything that fulfills contract n even classes that don’t exist yet! n Example: Comparable n useful if you need to sort items n compareTo(object) n returns int < 0 if this object less than parameter n returns 0 if same n returns int > 0 if this object greater than parameter

13 Comparable n sort method that works on array of objects of any type that implements Comparable n type guaranteed to have compareTo method n we need to sort n Bunny n Giraffe n String n...

14 Selection Sort For Int Primitives // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }

15 Wrappers n Many classes implement Comparable interface n Byte, Character, Double, Float, Integer, Long, Short, String n each implements own version of compareTo n Wrapper classes n wraps up (encapsulates) primitive type n Double: object wrapping primitive double n No: sort( double[] myData ); n Yes: sort( Double[] myData );

16 Multiple Interfaces n Classes can implement more than one interface at once n contract to implement all abstract methods defined in every interface it implements public class MyClass implements Interface1, Interface2, Interface3 { }