Академический Документы
Профессиональный Документы
Культура Документы
6/23/2011
Introduction
When we create a class that matches a specification, we say that the class implements the specification. In this chapter, we will visit four Java constructions for designing classes and connecting them to their collaborating classes. We will see how interfaces tie in with the rest of the constructs and through polymorphism, multiple inheritance, and abstraction enhance the functionality of Java.
6/23/2011
6/23/2011
Objectives:
1. Most Important: Learn to use interfaces in Java to think about code before you develop it. (Philosophy) 2. Learn how to use interfaces in Java (Mechanics) 3. Learn the differences between interfaces and abstract classes
6/23/2011
6/23/2011
6/23/2011
Java interfaces
Interfaces allow functionality to be shared between objects that agree to a contract on how the software should interact as a unit without needing knowledge of how the objects accomplish their task. On the CD player example, you have a contract with the manufacturer that once you press play, you will hear music. You do not know the specifics of how it is accomplished but you know what will happen. You have both agreed on an interface.
6/23/2011
Java interfaces
An interface is a reference type that can contain only nested types, method signatures and constants. Method bodies are not defined. Can not be instantiated. Can be implemented by other classes. Can be extended by other interfaces. All methods in an interface are automatically public abstract even if not explicitly stated as such.
10
6/23/2011
Java interfaces
Note that the method signature has no braces and is terminated by a semicolon. To use an interface you write a class that implements the interface.
11
6/23/2011
public class BMWCar implements OperateCar { int signalTurn(direction,speed){ // proprietary code to signal you are turning }
Java interfaces
A Toyota manufacturer may have different positioning of the signals on the car, different signal colors, different light intensity, but he is guaranteeing that when you use signalTurn you will get the same behavior as the BMW car.
12
6/23/2011
13
6/23/2011
Ignition: ??????
When a class inherits from two classes that implement common methods in different ways, what does it inherit? 14
6/23/2011
Runtime cannot determine whether it should invoke Ignition() from gas or diesel engines.
15
6/23/2011
Javas Solution
Java solves the diamond problem by not allowing multiple inheritance from ancestors that have any implementation details. Thus, in Java, classes can only multiply inherit from interfaces and abstract classes that have no implementation details.
16
6/23/2011
17
6/23/2011
How do we use the new features that were added in a new class implementation?
18
6/23/2011
class MySon implements MyKid { public void mayCry(){System.out.println(Its ok);} public void isSmart() {System.out.println(But he is certainly VERY smart); } }
A class can also implement more than one pertinent interface at the same time but can not inherit from more than one other class.
19
6/23/2011
implement all methods here. If there is a conflicting method defined in both interfaces the compiler will let us know !
20
6/23/2011
Abstract methods
You can declare an object without defining it:
Employee john;
Notice that the body of the method is missing ! A method that has been declared but not defined is an abstract method.
21
6/23/2011
Abstract Classes
Any class containing an abstract method is an abstract class. You must declare the class with the keyword abstract.
abstract class MyClass { };
An abstract class is incomplete. Its method bodies have missing implementations. You can not instantiate (create a new instance of) an abstract class.
22
6/23/2011
Abstract Classes
You can extend (subclass) an abstract class.
If the subclass defines all the inherited abstract methods, it is concrete and can be instantiated. If the subclass does not define all of the inherited abstract methods, it too needs to be defined abstract.
You can define a class to be abstract even if it does not contain any abstract methods.
This prevents the class from being instantiated.
You can have one or more methods provide full implementation if they are to be used frequently in the same manner. 23
6/23/2011
Abstract classes are used in providing a general category containing specific, concrete classes.
24
6/23/2011
Abstract Classes
public abstract class Shape { //can define constants public static final double TWO_PI=2*Math.PI; //can declare abstract methods public abstract double computeArea(); public abstract void draw(); //can implement concrete methods public String returnExample() {return euclidean;} }
25
6/23/2011
26
6/23/2011
27
6/23/2011
28
6/23/2011
Polymorphism
Polymorphism, reusability and extendibility tend to be the most common reasons that Java programmers use interfaces. They are closely related because all depend on the flexibility of inheriting from an interface. Designing to an interface often means designing at a high level of abstraction so that methods and classes with a high degree of commonality can share structure and approaches.
29
6/23/2011
Sample Problem
Let us say that you are bored watching the fish tank screen saver on your computer and want a view that shows animals walking, birds flying, and fish swimming on your screen at the same time. Here is an approach to designing with interfaces for polymorphism.
30
6/23/2011
Requirements
Here we are assuming that walk, swim and fly are all successive animations, but follow different rules. Walking can only take place on land and must stay in contact with the ground. Flying can only take place in the air. Swimming can only take place in the water.
31
6/23/2011
Interface Example
interface Mover { void move(); } abstract class Animal implements Mover { abstract public void move(); }
32
6/23/2011
33
6/23/2011
Further investigation
The preceding example is just getting started. An animation usually requires several images (different wing positions for a bird, etc.). There may be different kinds of birds. Each animal will have a current image and a current position on the screen. Each move will have a direction, velocity, and length of movement. How would you modify the interface to accommodate these specifications?
34
6/23/2011
References
http://www.cs.sjsu.edu/~kim/46b/contents/handouts/i nterfaces.doc http://www.apl.jhu.edu/Notes/LMBrown/resource/Abst ract-Classes.pdf http://www.cs.virginia.edu/~horton/cs494/slides/javainterfaces-collections.ppt http://java.sun.com/docs/books/tutorial/java/IandI/cre ateinterface.html
35
6/23/2011
References
http://gee.cs.oswego.edu/dl/cpj/ifc.html#secAbs http://www.cs.umd.edu/~clin/MoreJava/Objects/abscl ass.html http://www.uweb.ucsb.edu/~cdecuir/Abstraction%20a nd%20Interfaces.html http://www.csie.ntnu.edu.tw/~ghhwang/course_slices/ PL/Java_interface_more.ppt http://cs.nyu.edu/courses/spring07/V22.0101002/10slide.ppt
36
6/23/2011
References
http://people.cis.ksu.edu/~schmidt/PPJv12pdf/ch9V1 2.pdf http://www.cis.upenn.edu/~matuszek/cit5912003/Lectures/28-abstract-classes.ppt http://people.clarkson.edu/~jets/cs242/fa07/slides/05. classDesign.ppt
37
6/23/2011