39
Java Interfaces 6 April 2016 OSU CSE 1

Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

  • Upload
    voquynh

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Java Interfaces

6 April 2016 OSU CSE 1

Page 2: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Conceptual Framework

• A firm conceptual foundation for understanding and designing modern software recognizes:– Modern software consists of potentially large

numbers of components that are composed into “larger” components/systems

– In Java, one ordinarily thinks of a class as a component, with its client-visible specification in the interface(s) it implements

6 April 2016 OSU CSE 2

Page 3: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Conceptual Framework

• A firm conceptual foundation for understanding and designing modern software recognizes:– Modern software consists of potentially large

numbers of components that are composed into “larger” components/systems

– In Java, one ordinarily thinks of a class as a component, with its client-visible specification in the interface(s) it implements

6 April 2016 OSU CSE 3

An interface contains a description of what

software does.

Page 4: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Conceptual Framework

• A firm conceptual foundation for understanding and designing modern software recognizes:– Modern software consists of potentially large

numbers of components that are composed into “larger” components/systems

– In Java, one ordinarily thinks of a class as a component, with its client-visible specification in the interface(s) it implements

6 April 2016 OSU CSE 4

A class contains a description of how the

software does it.

An interface contains a description of what

software does.

Page 5: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Anatomy of an Interface/**

* An informal Javadoc comment.

* [additional Javadoc comments as needed; in our

* case: the mathematical model, contract(s) for

* constructor(s), optional contract for iterator]

*/

public interface I {

// contract(s) for method(s)

}

6 April 2016 OSU CSE 5

Page 6: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Anatomy of an Interface/**

* An informal Javadoc comment.

* [additional Javadoc comments as needed; in our

* case: the mathematical model, contract(s) for

* constructor(s), optional contract for iterator]

*/

public interface I extends I1, I2 {

// contract(s) for method(s)

}

6 April 2016 OSU CSE 6

An interface may extend one or more

other interfaces.

Page 7: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Anatomy of an Interface/**

* An informal Javadoc comment.

* [additional Javadoc comments as needed; in our

* case: the mathematical model, contract(s) for

* constructor(s), optional contract for iterator]

*/

public interface I<T> extends I1<T>, I2 {

// contract(s) for method(s)

}

6 April 2016 OSU CSE 7

An interface may be generic, as seen here; formal parameter T may have any name, but T is

typical (for “type”).

Page 8: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Technicalities

• Interfaces may be used to define:– instance methods– static final variables (“constants”)– Not constructors– Not static methods– Not instance variables

6 April 2016 OSU CSE 8

Page 9: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Technicalities

• Interfaces may be used to define:– instance methods– static final variables (“constants”)– Not constructors– Not static methods– Not instance variables

6 April 2016 OSU CSE 9

All instance methods in an interface are

automatically public abstract.

Page 10: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Technicalities

• Interfaces may be used to define:– instance methods– static final variables (“constants”)– Not constructors– Not static methods– Not instance variables

6 April 2016 OSU CSE 10

Variables are automatically

public static final; it is unusual for an interface to define variables.

Page 11: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Technicalities

• Interfaces may be used to define:– instance methods– static final variables (“constants”)– Not constructors– Not static methods– Not instance variables

6 April 2016 OSU CSE 11

A constructor always has the name of a

class, and is not an instance method.

Page 12: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Technicalities

• Interfaces may be used to define:– instance methods– static final variables (“constants”)– Not constructors– Not static methods– Not instance variables

6 April 2016 OSU CSE 12

As of Java 8, interfaces allow static methods with their

implementations. But providing code in interfaces goes

against our use of interfaces for client views (this biases

designs to instance methods).

Page 13: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Technicalities

• Interfaces may be used to define:– instance methods– static final variables (“constants”)– Not constructors– Not static methods– Not instance variables

6 April 2016 OSU CSE 13

This is a good thing: instance variables are not client-view

information!

Page 14: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Example: Queue Component Family

6 April 2016 OSU CSE 14

Queue

QueueKernel

extends

Standard

extends

Iterable

extends

Page 15: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Example: Queue Component Family

6 April 2016 OSU CSE 15

Queue

QueueKernel

extends

Standard

extends

Iterable

extends

Which methods are in Standard,

and why?

Page 16: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Example: Queue Component Family

6 April 2016 OSU CSE 16

Queue

QueueKernel

extends

Standard

extends

Iterable

extends

We call the “core interface” in this design

style the kernel interface.

Page 17: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Example: Queue Component Family

6 April 2016 OSU CSE 17

Queue

QueueKernel

extends

Standard

extends

Iterable

extends

We call the “most powerful interface” in this design style the enhanced interface.

Page 18: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Let’s Examine...

• Standard

• QueueKernel

• Queue

6 April 2016 OSU CSE 18

Ask about details, e.g.:• Design choices (math

model, constructor, kernel methods, other methods)

• Javadoc (compare code in the Java interface with the generated documentation)

Page 19: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Interface Design• The kernel interface defines:

– The mathematical model for the type– Contract(s) for the constructor(s)– Contract(s) for kernel methods– Contract(s) for methods inherited from Java

library interfaces that do not have their own contract specifications (if applicable; e.g., an iterator or a comparator)

• The enhanced interface defines contracts for all other methods for the type

6 April 2016 OSU CSE 19

Page 20: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Kernel Design Guidelines

• Kernel methods generally should be:– A minimal set of methods that is functionally

complete, i.e., powerful enough to:• Give a variable of the type any allowable value• Determine the value of a variable of the type

– Powerful enough to allow a client to:• Implement equals and toString• Check every kernel method’s precondition

6 April 2016 OSU CSE 20

Page 21: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Kernel Design Guidelines

• Kernel methods generally should be:– A minimal set of methods that is functionally

complete, i.e., powerful enough to:• Give a variable of the type any allowable value• Determine the value of a variable of the type

– Powerful enough to allow a client to:• Implement equals and toString• Check every kernel method’s precondition

6 April 2016 OSU CSE 21

“Minimal” means if any proposed kernel method were left out, you

could not satisfy one of the completeness tests—though sometimes an “extra” kernel

method is so parsimonious it is included in the kernel anyway!

Page 22: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Notice: Circularitypublic interface QueueKernel<T>

extends Standard<Queue<T>>, Iterable<T> {

...

}

public interface Queue<T>extends QueueKernel<T> {

...

}

6 April 2016 OSU CSE 22

Page 23: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Notice: Circularitypublic interface QueueKernel<T>

extends Standard<Queue<T>>, Iterable<T> {

...

}

public interface Queue<T>extends QueueKernel<T> {

...

}

6 April 2016 OSU CSE 23

Using Queue<T> is necessary because it is the return type of newInstance and the interface type

we always use to declare Queue variables.

Page 24: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

What “Mentions” What?

6 April 2016 OSU CSE 24

Queue

QueueKernel

“mentions”

Standard

“mentions”

Iterable

“mentions”

“mentions”

Page 25: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Interface = Type

• Java permits the circularity here because:– A Java interface defines a type, i.e., the type

name (which we consider to denote a set of mathematical model values that certain variables might have) along with the set of its instance methods

– An interface type may be used in Java even if there is no implementation of it in scope

6 April 2016 OSU CSE 25

Page 26: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Interfaces: Only At Compile-Time

• Interfaces are a compile-time construct– Used by the Java compiler for type-checking

with declared types: to make sure variables are used only where they make sense

• Recall the rules for declared types and object (dynamic) types

– Once a Java program compiles, only object types are kept at run-time

• Declared types literally “disappear” in the JVM

6 April 2016 OSU CSE 26

Page 27: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Interfaces: Only At Compile-Time

• Interfaces are a compile-time construct– Used by the Java compiler for type-checking

with declared types: to make sure variables are used only where they make sense

• Recall the rules for declared types and object (dynamic) types

– Once a Java program compiles, only object types are kept at run-time

• Declared types literally “disappear” in the JVM

6 April 2016 OSU CSE 27

• The declared type of a variable may be an interface type• The object type can never be an interface type, because

you may not instantiate a variable using new followed by an interface type

• If the declared type is an interface type, then the object type (a class) must implement the declared type (an interface)

Page 28: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Javadoc Tags

• Recall that the short one-sentence informal overview, and the following standard Javadoc tags, are “expected”:– @param– @return

6 April 2016 OSU CSE 28

Page 29: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Javadoc Tags

• Recall that the short one-sentence informal overview, and the following standard Javadoc tags, are “expected”:– @param– @return

6 April 2016 OSU CSE 29

Checkstyle warns you if they are missing when they should be there.

Page 30: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Custom Javadoc Tags

• Javadoc supports custom tags that can be introduced to document various aspects not considered when Javadocwas introduced– Example: contracts

6 April 2016 OSU CSE 30

Page 31: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

OSU CSE Custom Contract Tags

6 April 2016 OSU CSE 31

Custom Tags forInterfaces

Custom Tags forClasses

@mathsubtypes @convention

@mathdefinitions @correspondence

@mathmodel

@initially

@iterator

Page 32: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

6 April 2016 OSU CSE 32

Custom Tags forConstructors and Methods

Custom Tags forLoops

@aliases @maintains

@updates @decreases

@replaces

@clears

@requires

@ensures

@decreases

OSU CSE Custom Contract Tags

Page 33: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Packages

• Each OSU CSE component family is bundled into its own package, i.e., a grouping of interfaces and classes that the designer thinks “belong together” for logical reasons– Example: the Queue-family components are

all in the package components.queue

6 April 2016 OSU CSE 33

Page 34: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Packages Are Useful• A package provides:

– Logical structuring: packages are hierarchical, i.e., you may have packages within packages

– A namespace: units in different packages may have the same name without conflict

• See also import statements

– Another level of access control between public and private

6 April 2016 OSU CSE 34

Page 35: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Packages Are Useful• A package provides:

– Logical structuring: packages are hierarchical, i.e., you may have packages within packages

– A namespace: units in different packages may have the same name without conflict

• See also import statements

– Another level of access control between public and private

6 April 2016 OSU CSE 35

But any apparent “control” is easily circumvented and illusory, so we do

not recommend using it.

Page 36: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Package Declarationpackage components.queue;

import components.standard.Standard;public interface Queue<T> ...

• A Java file must be in a file system directory matching the package name– Eclipse handles this correspondence for you

• At most one package declaration per file– If there is no package declaration,

interface/class is in unnamed default package

6 April 2016 OSU CSE 36

Page 37: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Package Declarationpackage components.queue;

import components.standard.Standard;public interface Queue<T> ...

• A Java file must be in a file system directory matching the package name– Eclipse handles this correspondence for you

• At most one package declaration in a file– If there is no package declaration,

interface/class is in unnamed default package

6 April 2016 OSU CSE 37

This line declares that the interface (or class) in the current file belongs to the package components.queue.

Page 38: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Package Declarationpackage components.queue;

import components.standard.Standard;public interface Queue<T> ...

• A Java file must be in a file system directory matching the package name– Eclipse handles this correspondence for you

• At most one package declaration in a file– If there is no package declaration,

interface/class is in unnamed default package

6 April 2016 OSU CSE 38

This line brings the interface Standard into scope, from the

package components.standard.

Page 39: Java Interfaces - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/29...Java Interfaces. 6 April 2016 OSU CSE 1. ... – An interface type

Resources

• Big Java Late Objects, Section 9.6: Interface Types– http://proquest.safaribooksonline.com.proxy.lib.ohio-

state.edu/book/programming/java/9781118087886/chapter-9-inheritance-and-interfaces/navpoint-86

• javadoc — The Java API Documentation Generator– http://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html

6 April 2016 OSU CSE 39