Top Banner
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott
35

Chapter 2 Introducing Interfaces

Feb 22, 2016

Download

Documents

ananda ananda

Chapter 2 Introducing Interfaces. Summary prepared by Kirk Scott. A class’s (public) interface is the collection of methods that can be called on it (If you declare instance variables public, they also become part of the interface) - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 2 Introducing Interfaces

Chapter 2Introducing Interfaces

Summary prepared by Kirk Scott

Page 2: Chapter 2 Introducing Interfaces

• A class’s (public) interface is the collection of methods that can be called on it

• (If you declare instance variables public, they also become part of the interface)

• You can think of the interface as a contract or commitment to do what a descriptive method name implies

• The implementation of the interface consists of the code for the methods

Page 3: Chapter 2 Introducing Interfaces

• As you know, Java elevates the idea of an interface to a separate concept

• A Java interface consists of the specification of the methods

• A class which implements the interface is committed to providing code which performs the desired actions for those methods

• Separating the interface specifications into a separate construct makes two things possible:

Page 4: Chapter 2 Introducing Interfaces

• 1. Two different classes can be declared to implement the same interface

• This means that instances of those two classes can play the same role in various pieces of code

• In other words, not only can you have a superclass reference

• You can also have an interface reference in code

Page 5: Chapter 2 Introducing Interfaces

• 2. A given class can implement more than one interface

• As pointed out in CSCE 202, interfaces are a substitute for multiple inheritance in Java

• More generally, you can observe this:• It means that an object of a given class can

play different roles in different pieces of software, depending on its interface reference

Page 6: Chapter 2 Introducing Interfaces

• Interfaces allow for great flexibility in code design and implementation

• The simple idea of an interface reference can be extremely useful

• Interfaces also form the foundation for a number of different design patterns

Page 7: Chapter 2 Introducing Interfaces

Interfaces and Abstract Classes

• C++ and Smalltalk do not have interfaces• They have abstract classes• Java interfaces can be described as completely

abstract classes• Some authors don’t like that comparison, but

in this book it is considered acceptable

Page 8: Chapter 2 Introducing Interfaces

• Challenge 2.1• Write down three differences between

abstract classes and interfaces in Java

Page 9: Chapter 2 Introducing Interfaces

• Solution 2.1• An abstract class with no nonabstract

methods is similar to an interface in terms of its utility.

• However, note the following.• A class can implement any number of

interfaces but can subclass at most one abstract class

Page 10: Chapter 2 Introducing Interfaces

• An abstract class can have nonabstract methods; al the methods of an interface are effectively abstract.

• An abstract class can declare and use fields; an interface cannot, although it can create static final constants

Page 11: Chapter 2 Introducing Interfaces

• An abstract class can have methods whose access is public, protected, private, or none (package).

• An interface’s methods are implicitly public.• An abstract class can define constructors; an

interface cannot.

Page 12: Chapter 2 Introducing Interfaces

• It is possible to write object-oriented software without interfaces, using a language like C++

• However, the existence of interfaces in Java is a net plus

• The book asserts that they are particularly useful in n-tier (multi-tier, client-server) development

Page 13: Chapter 2 Introducing Interfaces

• The book next gives a moderately garbled interface definition in order to pose the questions in the next challenge

• This may not be the best way to emphasize the points they want to bring out, but the answers to the challenge do include some new information

• Consider the interface definition on the following overhead:

Page 14: Chapter 2 Introducing Interfaces

• package com.oozinoz.simulation;

• public interface RocketSim• {• abstract double getMass();• public double getThrust();• void setSimTime(double t);• }

Page 15: Chapter 2 Introducing Interfaces

• Challenge 2.2• Which of the following statements are true?• [Rather than putting all of the answers at the

end, the challenge is presented in the form question/answer.]

Page 16: Chapter 2 Introducing Interfaces

• A. All three methods of the RocketSim interface are abstract, although only getMass() declares this explicitly.

• True.• Interface methods are always abstract,

whether or not they declare it.

Page 17: Chapter 2 Introducing Interfaces

• B. All three methods of the interface are public, although only getThrust() declares this explicitly.

• True.• Interface methods are public, whether or not

they declare it.

Page 18: Chapter 2 Introducing Interfaces

• C. The interface is declared “public interface”, but it would be public even if the public keyword were omitted.

• False!• An interface’s visibility may be limited to the

package in which it resides.• In this case, it is marked public, so classes

outside com.oozinoz.simulation can access it.

Page 19: Chapter 2 Introducing Interfaces

• D. It is possible to create another interface, say, RocketSimSolid, that extends RocketSim.

• True.• For example, the List and Set interfaces both

extend the Collection interface in java.util.

Page 20: Chapter 2 Introducing Interfaces

• E. Every interface must have at least one method.

• False.• An interface with no methods is known as a

marker interface.• Sometimes, a method high in a class hierarchy,

such as Object.clone(), is not appropriate for every subclass.

Page 21: Chapter 2 Introducing Interfaces

• You can create a marker interface that requires subclasses to opt in or opt out of participation in such a scheme.

• The clone() method on Object requires subclasses to opt in, by declaring that they implement the Cloneable marker interface.

Page 22: Chapter 2 Introducing Interfaces

• F. An interface can declare instance fields that an implementing class must also declare.

• False.• An interface cannot declare instance fields,

although it can create constants by declaring fields that are static and final.

Page 23: Chapter 2 Introducing Interfaces

• G. Although you can’t instantiate an interface, an interface definition can declare constructor methods that require an implementing class to provide constructors with given signatures.

• False.• It might be a good idea, but there is no way for

a Java interface to require that implementing classes provide a particular constructor.

Page 24: Chapter 2 Introducing Interfaces

Interfaces and Obligations

• The utility of interfaces, both generic public interfaces, and interfaces separately defined, comes from the idea of encapsulation

• On the one hand, code that uses a class cannot mess with the contents of the class

• On the other hand, code that uses a class doesn’t care how methods are implemented

Page 25: Chapter 2 Introducing Interfaces

• These are constraints that limit the interaction between classes

• But the limitation is very helpful• One class only needs to know the following:• What is the interface for the other class• What does the interface support (hopefully the

method names are descriptive)• How is the interface used (accessibility,

parameters, return types, etc.)

Page 26: Chapter 2 Introducing Interfaces

• In general, say with RocketSim, the developer is obligated to implement methods that fulfill the “contract” implied by the interface

• In other words, getMass() and getThrust() really ought to return correct values for these quantities for a given object of a class that implements the simulation interface

Page 27: Chapter 2 Introducing Interfaces

• There is another, special case, where the implementer of an interface is not providing a service to the caller

• It turns out that implementing the interface makes it possible, in effect, for the caller to provide a service to the code that’s called

Page 28: Chapter 2 Introducing Interfaces

• You will discover that you are already familiar with this situation

• The book introduces it as a challenge• Because you’ve probably never thought about

it in this way, as usual, you can’t predict what the answer to the challenge will be

Page 29: Chapter 2 Introducing Interfaces

• Challenge 2.3• Give an example of an interface with methods

that do not imply responsibility on the part of the implementing class to return a value or even to take any action on behalf of the caller at all.

Page 30: Chapter 2 Introducing Interfaces

• Solution 2.3• One example occurs when classes may be

registered as listeners for events;• the classes receive notification for their own

benefit, not the caller’s.• For example, we may want to take action on

MouseListener.mouseDragged() but have an empty body for MouseListener.mouseMoved() for the same listener.

Page 31: Chapter 2 Introducing Interfaces

• You are familiar with the Java API approach to making this simpler

• In addition to a MouseListener interface, there is a MouseAdapter class

• Instead of implementing the interface and providing bogus method implementations, you can extend the class, inherit bogus implementations, and override only those of interest

Page 32: Chapter 2 Introducing Interfaces

• The book returns to the idea of constants in interfaces, giving a concrete example:

• public interface ClassificationConstants

• {• static final int CONSUMER = 1;• static final int DISPLAY = 2;• }

Page 33: Chapter 2 Introducing Interfaces

Summary

• [Direct quote from book.]• The power of interfaces is that they delineate

what is and isn’t expected in how classes collaborate.

• Interfaces are similar to purely abstract classes, defining expectations but not implementing them.

Page 34: Chapter 2 Introducing Interfaces

• Mastering both the concepts and details of applying Java interfaces is well worth the investment of your time.

• This powerful construct is at the heart of many strong designs and several design patterns.

Page 35: Chapter 2 Introducing Interfaces

The End