CS61B Lecture #9: Interfaces and Abstract Classes. Recreation. Abstract Methods and Classes. Methods on Drawables

Similar documents
CS61B Lecture #10: OOP mechanism and Class Design. Last modified: Thu Sep 13 16:38: CS61B: Lecture #10 1

CS61B Lecture #9: Abstract Classes and All That

CS61B Lecture #9: Interfaces and Abstract Classes

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

CS61B Lecture #7. Announcements:

CSE 331 Final Exam 3/16/15 Sample Solution

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS61B Lecture #12. Public Service Announcement. Miscellaneous Topics: What to do About Errors? throw new SomeException (optional description);

CS61B Lecture #13: Packages, Access, Etc.

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Last name:... First name:... Department (if not D-INFK):...

CS61B Lecture #13: Packages, Access, Etc.

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

Topic 7: Algebraic Data Types

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division DO NOT. P. N.

Comments are almost like C++

Java Object Oriented Design. CSC207 Fall 2014

Fundamentals of Object Oriented Programming

CS61B Lecture #5: Arrays and Objects

6.170 Recitation #5: Subtypes and Inheritance

CS 11 java track: lecture 3

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

CS 61B Discussion 5: Inheritance II Fall 2014

INSTRUCTIONS TO CANDIDATES

Abstract. 1. What is an ABSTRACT METHOD? 2. Why you would want to declare a method as abstract? 3. A non-abstract CLASS is called a concrete class

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Code Documentation.

Programming Languages and Techniques (CIS120)

Making New instances of Classes

Rules and syntax for inheritance. The boring stuff

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 10 Classes Continued. Fundamentals of Java

CS/ENGRD 2110 SPRING 2018

COMP6700/2140 Methods

G Programming Languages - Fall 2012

ITI Introduction to Computing II

CS61B Lecture #8: Object-Oriented Mechanisms

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

ITI Introduction to Computing II

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Classes, interfaces, & documentation. Review of basic building blocks

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Assertions, pre/postconditions

Chapter 4 Defining Classes I

int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; }

Universe Type System for Eiffel. Annetta Schaad

CSE 331 Midterm Exam Sample Solution 2/18/15

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

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

index.pdf January 21,

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Collections, Maps and Generics

CS 177 Week 15 Recitation Slides. Review

ITI Introduction to Computing II

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CSCI-GA Scripting Languages

1.00 Lecture 14. Exercise: Plants

For this section, we will implement a class with only non-static features, that represents a rectangle

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger and Josh Hug

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Subtypes and Subclasses

Largest Online Community of VU Students

Assignment 2 - Specifications and Modeling

CSE 143 Lecture 20. Circle

A Short Summary of Javali

C++ Inheritance and Encapsulation

OOP: Key Concepts 09/28/2001 1

ITI Introduction to Computing II

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Chapter 14 Abstract Classes and Interfaces

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

CS 360: Programming Languages Lecture 12: More Haskell

ITI Introduction to Computing II

Inheritance, and Polymorphism.

Principles of Programming Languages

Use the scantron sheet to enter the answer to questions (pages 1-6)

Lecture 02, Fall 2018 Friday September 7

CSCI 102 Fall 2010 Exam #1

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Design Patterns: State, Bridge, Visitor

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

CSE 341, Autumn 2015, Ruby Introduction Summary

F I N A L E X A M I N A T I O N

CSE 12 Abstract Syntax Trees

Exception-Handling Overview

Transcription:

Recreation Show that for any polynomial with a leading coefficient of 1 and integral coefficients, all rational roots are integers. CS61B Lecture #9: Interfaces and Abstract Classes Announcements Berkeley Programming Contest: Sat., 3 October at 10:00 AM. Summary of readings on current topic up to now: Chapters 7 9 of Head-First Java. Reminder: Projects are individual efforts: The four projects are individual efforts in this class (no partnerships). Feel free to discuss projects or pieces of them before doing the work. But you must complete and write up each project yourself. That is, feel free to discuss projects with each other, but be aware that we expect your work to be substantially different from that of all your classmates (in this or any other semester). Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 1 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 2 Abstract Methods and Classes Instance method can be abstract: No body given; must be supplied in subtypes. One good use is in specifying a pure interface to a family of types: /** A drawable object. */ public abstract class Drawable { // "abstract" = "can t say new Drawable" /** Expand THIS by a factor of SIZE */ public abstract void scale(double size); /** Draw THIS on the standard output. */ public abstract void draw(); Now a Drawable is something that has at least the operations scale and draw on it. Can t create a Drawable because it s abstract in particular, it has two methods without any implementation. Methods on Drawables /** A drawable object. */ public abstract class Drawable { // "abstract" = "can t say new Drawable" /** Expand THIS by a factor of SIZE */ public abstract void scale(double size); /** Draw THIS on the standard output. */ public abstract void draw(); Can t write new Drawable(), because it would have unimplemented methods. BUT, we can write methods that operate on Drawables in Drawable or in other classes: void drawall(drawable[] thingstodraw) { for (Drawable thing : thingstodraw) thing.draw(); But draw has no implementation! How can this work? Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 3 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 4

Concrete Subclasses Regular classes can extend abstract ones to make them less abstract by overriding their abstract methods. CandefinekindsofDrawablesthat areconcrete,in thatall methods have implementations and one can use new on them: public class Rectangle extends Drawable { public Rectangle(double w, double h) { this.w = w; this.h = h; public void scale(double size) { w *= size; h *= size; public void draw() { draw a w x h rectangle private double w,h; Any Circle or Rectangle is a Drawable. public class Circle extends Drawable { public Circle(double rad) { this.rad = rad; public void scale(double size) { rad *= size; public void draw() { draw a circle with radius rad private double rad; Using Concrete Classes We can create new Rectangles and Circles. Since these classes are subtypes of Drawable, we can put them in any container whose static type is Drawable,......andthereforecanpassthemtoanymethodthatexpectsDrawable parameters: Thus, writing Drawable[] things = { new Rectangle(3, 4), new Circle(2) ; drawall(things); draws a 3 4 rectangle and a circle with radius 2. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 5 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 6 Interfaces In generic use, an interface is a point where interaction occurs between two systems, processes, subjects, etc. (Concise Oxford Dictionary). In programming, often use the term to mean a description of this generic interaction, specifically, a description of the functions or variables by which two things interact. Java uses the term to refer to a slight variant of an abstract class that contains only abstract methods(and static constants), like this: public interface Drawable { void scale(double size); // Automatically public. void draw(); Interfaces are automatically abstract: can t say new Drawable(); can say new Rectangle(...). Implementing Interfaces Idea is to treat Java interfaces as the public specifications of data types, and classes as their implementations: public class Rectangle implements Drawable {... Can use the interface as for abstract classes: void drawall(drawable[] thingstodraw) { for (Drawable thing : thingstodraw) thing.draw(); Again, this works for Rectangles and any other implementation of Drawable. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 7 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 8

Multiple Inheritance Can extend one class, but implement any number of interfaces. Contrived Example: interface Readable { void copy(readable r, Object get(); Writable w) { w.put(r.get()); interface Writable { void put(object x); class Source implements Readable { class Sink implements Writable { public Object get() {... public void put(object x) {... class Variable implements Readable, Writable { public Object get() {... public void put(object x) {... The first argument of copy can be a Source or a Variable. The second can be a Sink or a Variable. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 9 Review: Higher-Order Functions In Python, you had higher-order functions like this: def map(proc, items): function list if items is None: return None else: return IntList(proc(items.head), map(proc, items.tail)) and could write zmap(abs, makelist(-10, 2, -11, 17)) ====> makelist(10, 2, 11, 17) map(lambda x: x * x, makelist(1, 2, 3, 4)) ====> makelist(t(1, 4, 9, 16) Java does not have these directly, but can use abstract classes or interfaces and subtyping to get the same effect (with more writing) Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 10 Map in Java /** Function with one integer argument */ IntList map(intunaryfunction proc, IntList items) { public interface IntUnaryFunction { if (items == null) int apply(int x); return null; else return new IntList( proc.apply(items.head), map(proc, items.tail) ); It s the use of this function that s clumsy. First, define class for absolute value function; then create an instance: class Abs implements IntUnaryFunction { public int apply(int x) { return Math.abs(x); ---------------------------------------------- R = map(new Abs(), some list); Lambda Expressions InJava 7, onecancreateclasseslikesabsontheflywithanonymous classes: R = map(new IntUnaryFunction() { public int apply(int x) { return Math.abs(x);, some list); This is sort of like declaring class Anonymous implements IntUnaryFunction { public int apply(int x) { return x*x; and then writing R = map(new Anonymous(), some list); In Java 8, this is even more succinct: R = map((int x) -> Math.abs(x), some list); or even better, when the function already exists: R = map(math::abs, some list); These figure out you need an anonymous IntUnaryFunction and create one. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 11 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 12

Review: A Puzzle class A { void f() { System.out.println("A.f"); void g() { f(); /* or this.f() */ //static void g(a y) { y.f(); class C { static void main(string[] args) { B ab = new B(); h(ab); static void h(a x) { x.g(); //static void h(a x) { A.g(x); // x.g(x) also legal here 1. What is printed? 2. What if we made g static? 3. What if we made f static? 4. What if f were not defined in A? class B extends A { void f() { System.out.println("B.f"); Choices: a. A.f b. B.f c. Some kind of error Answer to Puzzle 1. Executing java C prints, because A. C.main calls h and passes it ab, whose dynamic type is B. B. h calls x.g(). Since g is inherited by B, we execute the code for g in class A. C. g calls this.f(). Now this contains the value of h s argument, whose dynamic type is B. Therefore, we execute the definition of f that is in B. D. In calls to f, in other words, static type is ignored in figuring out what method to call. 2. If g were static, we see ; selection of f still depends on dynamic type of this. 3. If f were static, would print because then selection of f would depend on static type of this, which is A. 4. If f were not defined in A, we d get. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 13 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 14 Example: Designing a Class Problem: Want a class that represents histograms, like this one: 0.0-0.2 0.2-0.4 0.4-0.6 0.6-0.8 0.8-1.0 Analysis: What do we need from it? At least: Specify buckets and limits. Accumulate counts of values. Retrieve counts of values. Retrieve numbers of buckets and other initial parameters. Specification Seen by Clients The clients of a module (class, program, etc.) are the programs or methods that use that module s exported definitions. In Java, intention is that exported definitions are designated public. Clients are intended to rely on specifications, (aka APIs) not code. Syntactic specification: method and constructor headers syntax needed to use. Semantic specification: what they do. No formal notation, so use comments. Semantic specification is a contract. Conditions client must satisfy (preconditions, marked Pre: in examples below). Promised results (postconditions). Design these to be all the client needs! Exceptions communicate errors, specifically failure to meet preconditions. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 15 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 16

Histogram Specification and Use An Implementation /** A histogram of floating-point values */ public interface Histogram { /** The number of buckets in THIS. */ int size(); /** Lower bound of bucket #K. Pre: 0<=K<size(). */ double low(int k); /** # of values in bucket #K. Pre: 0<=K<size(). */ int count(int k); /** Add VAL to the histogram. */ void add(double val); Sample output: >= 0.00 10 >= 10.25 80 >= 20.50 120 >= 30.75 50 public class FixedHistogram implements Histogram { private double low, high; /* From constructor*/ private int[] count; /* Value counts */ /** A new histogram with SIZE buckets recording values >= LOW and < HIGH. */ public FixedHistogram(int size, double low, double high) { if (low >= high size <= 0) throw new IllegalArgumentException(); this.low = low; this.high = high; this.count = new int[size]; public int size() { return count.length; public double low(int k) { return low + k * (high-low)/count.length; void fillhistogram(histogram H, Scanner in) { while (in.hasnextdouble()) H.add(in.nextDouble()); void printhistogram (Histogram H) { for (int i = 0; i < H.size(); i += 1) System.out.printf (">=%5.2f %4d%n", H.low(i), H.count(i)); public int count(int k) { return count[k]; public void add(double val) { int k = (int) ((val-low)/(high-low) * count.length); if (k >= 0 && k < count.length) count[k] += 1; Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 17 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 18 Don t require a priori bounds: Let s Make a Tiny Change class FlexHistogram implements Histogram { /** A new histogram with SIZE buckets. */ public FlexHistogram(int size) {? // What needs to change? How would you do this? Profoundly changes implementation. But clients (like printhistogram and fillhistogram) still work with no changes. Illustrates the power of separation of concerns. Implementing the Tiny Change Pointless to pre-allocate the count array. Don t know bounds, so must save arguments to add. Then recompute count array lazily when count( ) called. Invalidate count array whenever histogram changes. class FlexHistogram implements Histogram { private List<Double> values =...; // Java library type (later) int size; private int[] count; public FlexHistogram(int size) { this.size = size; this.count = null; public void add(double x) { count = null; values.add(x); public int count(int k) { if (count == null) { compute count from values here. return count[k]; Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 19 Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 20

Advantages of Procedural Interface over Visible Fields By using public method for count instead of making the array count visible, the tiny change is transparent to clients: If client had to write myhist.count[k], would mean Thenumberofitemscurrentlyinthe k th bucketofhistogram myhist (and by the way, there is an array called count in myhist that always holds the up-to-date count). Parenthetical comment useless to the client. But if count array had been visible, after tiny change, every use of count in client program would have to change. So using a method for the publiccount decreases what client has to know, and (therefore) has to change. Last modified: Fri Sep 18 03:21:17 2015 CS61B: Lecture #9 21