INTERFACES IN JAVA. Prof. Chris Jermaine

Similar documents
INTERFACES IN JAVA. Prof. Chris Jermaine

A POST-MORTEM OF A2. Prof. Chris Jermaine

EXCEPTIONS. Prof. Chris Jermaine

INHERITANCE AND TYPE HIERARCHIES. Prof. Chris Jermaine

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Interfaces & Generics

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Basic Data Structures

Java Collection Framework

Basic Data Structures 1 / 24

Fun facts about recursion

CS/ENGRD 2110 SPRING 2018

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

COMP 250 Winter stacks Feb. 2, 2016

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

LINKED STRUCTURES IN JAVA (#2) Prof. Chris Jermaine

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Array Based Lists. Collections

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

COP 3330 Final Exam Review

ITI Introduction to Computing II

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

COMP1008 Exceptions. Runtime Error

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

16 Multiple Inheritance and Extending ADTs

CSE 100: GRAPH ALGORITHMS

Linked Structures. See Section 3.2 of the text.

CS Introduction to Data Structures How to Parse Arithmetic Expressions

cs Java: lecture #6

CSIS 10B Lab 2 Bags and Stacks

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Stacks and Queues

CMSC330. Objects, Functional Programming, and lambda calculus

COMP 401 Fall Recitation 7: Factories and Lists

CS61BL Summer 2013 Midterm 2

CITS1001 week 4 Grouping objects

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 7: Data Abstractions

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Designing Robust Classes

Queues. Stacks and Queues

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

These are notes for the third lecture; if statements and loops.

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Problem 1: Building and testing your own linked indexed list

Exceptions and Design

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Unit 4: Client View of a Component Methods

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

+ Abstract Data Types

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

For instance, we can declare an array of five ints like this: int numbers[5];

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CS18000: Programming I

COMP 215: INTRO TO PROGRAM DESIGN. Prof. Chris Jermaine Chris Prof. Chris Dr. Chris

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

The Dynamic Typing Interlude

G Programming Languages - Fall 2012

ITI Introduction to Computing II

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

17 Multiple Inheritance and ADT Extensions

CS 211. Project 5 Infix Expression Evaluation

Proofwriting Checklist

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CITS1001 week 4 Grouping objects lecture 2

Checked and Unchecked Exceptions in Java

More Data Structures (Part 1) Stacks

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Concurrent Objects and Linearizability

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Garbage Collection (1)

Model Solutions. COMP 103: Test April, 2013

Intro. Scheme Basics. scm> 5 5. scm>

Comp215: Thinking Generically

[2:3] Linked Lists, Stacks, Queues

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Data Structure. Recitation VII

Rules and syntax for inheritance. The boring stuff

CS 251 Intermediate Programming Inheritance

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

CS 221 Review. Mason Vail

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

CS558 Programming Languages

Transcription:

INTERFACES IN JAVA Prof. Chris Jermaine cmj4@cs.rice.edu 1

Now On To Interfaces in Java Java gives the ability to declare an interface Like a, except: Can t declare any member variables (well, you can, but don t do it) All functions are implicitly abstract, public So no implementations for anything! Example: Iterator in Java standard library: interface Iterator <E> { // note: this is a generic interface boolean hasnext (); // parameterized on type E E next (); void remove (); } So why does Java have these? 2

Interfaces vs. Abstract Classes Like an abstract Very similar, but are a few key differences A implements an interface, vs. extends another. Ex: IntArrayIter implements Iterator <Integer> { } Or, if Iterator hadn t been generic: IntArrayIter implements Iterator { } 3

Interfaces vs. Abstract Classes Like an abstract Very similar, but are a few key differences A implements an interface, vs. extends another. Ex: IntArrayIter implements Iterator <Integer> { } Or, if Iterator hadn t been generic: IntArrayIter implements Iterator { } Another key difference: multiple inheritance is allowed: IntArrayIter implements Iterator <.>, IResettable { } // IResettable might look like: interface IResettable { void startover (); } 4

Interfaces vs. Abstract Classes Very similar, but are a few key differences A implements an interface, vs. extends another. Ex: IntArrayIter implements Iterator <Integer> { } Or, if Iterator hadn t been generic: IntArrayIter implements Iterator { } Another key difference: multiple inheritance is allowed: IntArrayIter implements Iterator <.>, IResettable { } // IResettable might look like: interface IResettable { void startover (); } Is this a good idea here? 5

Don t Abuse Multiple Inheritance! In this example, would have been much better to extend Iterator : interface IResettable <T> extends Iterator <T> { void startover (); } IntArrayIter implements IResettable <Integer> { } Why is this better? 6

Don t Abuse Multiple Inheritance! In this example, would have been much better to extend Iterator : interface IResettable <T> extends Iterator <T> { void startover (); } IntArrayIter implements IResettable <Integer> { } Why is this better? We really are adding to the interface... IResettable does not make much sense outside the context of Iterator Use multiple inheritance only if a really provides many, totally separate types of functionality 7

So, Why Do We Like Interfaces? It s very useful to have the ability to write code whose only purpose is to describe functionality No implementaton needed or wanted Abstraction in its purest form 8

Question: When Should You Use Abstract Classes, Interfaces, Regular Classes Typically, you will use all three: Interface Abstract Abstract 9

What Do You Put In An Interface? All of the functionality that is so abstract... That it has nothing to do with an implementation Examples: A stack has push and pop A queue has enqueue and dequeue A map has put and get 10

What Do You Put In An Abstract Class? Here you ll imp functionality that many/all imps will share Often, it will be interface functions that can be written in terms of other interface functions Try hard to pull functionality into the abstract Avoids repeating work! Out of the following methods: void push (int); int pop (); int size (); void reverse (); Which one would most likely go in the an abstract? 11

What Do You Put In An Abstract Class? Might put reverse in there: abstract AIntStack implements IInstStack { } public abstract void push (int i); public abstract int pop (); public abstract int size (); public void reverse () {???? } 12

Abstract Ops Not In Interface Why? Can implement reverse using just push and pop How? 13

Abstract Ops Not In Interface Why? Can implement reverse using just push and pop How? abstract AIntStack implements IInstStack { public void reverse () { if (size ()!= 0) { int i = pop (); reverse (); addatbottom (i); } } } private void addatbottom (int i) { if (size () == 0) push (i); else { int j = pop (); addatbottom (i); push (j); } } 14

Abstract Ops Not In Interface Note that this requires an addatbottom routine Pretty slow, since linear time in terms of push and pop Instead, could just require the concrete to implement it Since many implementations are going to be constant time Example: abstract AIntStack implements IInstStack { public abstract void push (int i); public abstract int pop (); public abstract int size (); protected abstract void addatbottom (int i); public void reverse () {... } } 15

Abstract Ops Not In Interface Note that this requires an addatbottom routine Pretty slow, since linear time in terms of push and pop Instead, could just require the concrete to implement it Since many implementations are going to be constant time Example: abstract AIntStack implements IInstStack { public abstract void push (int i); public abstract int pop (); public abstract int size (); protected abstract void addatbottom (int i); public void reverse () {... } } Note: if happy with linear, then leave imp in abstract can over-ride if desired 16

What Do You Put In The Class? The actual implementation! Ex: can implement AIntStack using ArrayList <Integer> : push (i) uses list.add (i) (adds integer at end of list) pop () uses list.remove (list.length () - 1) isempty () uses list.length () == 0 addatbottom adds to front of the list (still linear time!) 17

Now Time for Some Navel Gazing Will you actually ever have this? Interface Abstract Abstract Or is it always this: Interface Abstract 18

Now Time for Some Navel Gazing Will you actually ever have this? Abstract Interface Abstract This one should be pretty rare! Or is it always this: Interface Abstract 19

Why Avoid Multiple Abstract Imps? Often this is the knee-jerk reaction to multiple implementations of interface ops that can be written in terms of other interface ops But this can be problematic Imagine multiple stack implementations Along with multiple implementations of reverse Where all are interchangeable What happens if use multiple abstract solution, put each in diff abstract? 20

Why Avoid Multiple Abstract Imps? Often this is the knee-jerk reaction to multiple implementations of interface ops that can be written in terms of other interface ops But this can be problematic Imagine multiple stack implementations Along with multiple implementations of Reverse Where all are interchangeable What happens if use multiple abstract solution, put each in diff abstract? End up repeating concrete implementations with each abstract! 21

Why Avoid Multiple Abstract Imps? Often this is the knee-jerk reaction to multiple implementations of interface ops that can be written in terms of other interface ops But this can be problematic Imagine multiple stack implementations Along with multiple implementations of Reverse Where all are interchangeable What happens if use multiple abstract solution, put each in diff abstract? End up repeating concrete implementations with each abstract! What should we have done? 22

Why Avoid Multiple Abstract Imps? Often this is the knee-jerk reaction to multiple implementations of interface ops that can be written in terms of other interface ops But this can be problematic Imagine multiple stack implementations Along with multiple implementations of Reverse Where all are interchangeable What happens if we use the multiple abstract solution? What should we have done? IStack AStack reverser concrete stack imps is of type IStackReverser AStackReverser ReverseAlg1 ReverseAlg2 23

Do We Ever Actually Need Mult Abstracts? What if diff implementations of reverse depend on different ops... And those ops are not gonna be common to all imps? Imagine multiple stack implementations Easy to implement addatbottom for some imps Easy to implement replacevalatposi (val, pos) for other imps (An aside: what does reverse look like using replacevalatposi?) It s clear we can t use prior design strategy here In this case, should you have multiple abstract es? Not clear... What might we have done instead? 24

A Better Design? IStack IPrependableStack IItemReplacableStack APrependableStack es AItemReplacableStack es reverse imp goes here Sooo... is this better? I d say: it depends 25

Last Bit of Navel Gazing Should you always have a complete hierarchy? Interface, abstract base, (multiple?) concrete Some will argue emphatically YES! Perhaps even in later es you ll be told this? I ll be a little more permissive Either have one level (only a single concrete base) Or three or more (that is, never start with an abstract base) Why? 26

Last Bit of Navel Gazing Should you always have a complete hierarchy? Interface, abstract base, (multiple?) concrete Some will argue emphatically YES! Perhaps even in later es you ll be told this? I ll be a little more permissive Either have one level (only a single concrete base) Or three or more (that is, never start with an abstract base) Why? Sometimes you ll have a where you know you ll never need mult. imps Just make sure to switch over at first sign of trouble! But if you ve got an abstract base, just define the interface 27

This Sort of Does It For the Formal Intro To OODesign Talked about how one uses inheritance, polymorphism, and the proper role of interfaces You ll get this much more rigorously in COMP 310 Will distill many of the ideas we ve discussed here into design patterns Closing throughts I know many of you are skpetical. But keep in mind: 28

This Sort of Does It For the Formal Intro To OODesign Talked about how one uses inheritance, polymorphism, and the proper role of interfaces You ll get this much more rigorously in COMP 310 Will distill many of the ideas we ve discussed here into design patterns Closing throughts I know many of you are skpetical. But keep in mind: I ll grant you that the best programmers can write 50K SLOC in a year......and never think explicitly about design......and the code just works perfectly HOWEVER, most people (inluding, probably, you!) just are not that good And even if you are, heaven forbid you ever move on... 29

We Finish Up With Some Notes on A2 Goal is to implement the IDoubleVector interface Having a vector of doubles is a key abstraction to implementing our ML algorithm We ll need two actual implementations A dense one (built on top of array of doubles) A sparse one (built on top of ISparseArray generic, which you ll imp next) What s the diff between a dense array and a sparse array? 30

Need to Have a Notion of a Backing Value What s that? It s the default value for entry in an IDoubleVector Every non-empty slot is actually a delta or diff from the backing value Allows you to add same number to every entry in constant time Vital for sparse vector, might as well use for dense one, too 31

Most Ops Are Self-Explanatory In case you haven t seen it, the L1 norm of a vector... Is the sum of the absolute values of the entries in the vector Noramlization... Means you scale all of the entries to the L1 norm is one Keeping all ratios constant 32

A Super-Quick Note on Java Exceptions An exception tells caller that there was a problem in a method Caller is forced to handle this using the try-catch framework Look at test code to see this Many of the IDoubleVector ops throw OutOfBoundsException This means that whenever someone does something out of range... You should execute the line: throw new OutOfBoundsException (); Much more on exceptions later on... 33