Slides - Gustavus Adolphus College

Download Report

Transcript Slides - Gustavus Adolphus College

MCS 270 Spring 2014
Object-Oriented Software Development
MCS 270 Object-Oriented Software Development
Today’s schedule
Homework: Due today
Java Review:
Abstract Classes
Interfaces
Exceptions
Example: Tic Tac Toe from UML
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Abstract methods
You can declare an object without defining it:
Person p;
Similarly, you can declare a method without defining
it (no body):
public abstract void draw(int size);
A method that has been declared but not defined is
an abstract method
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Abstract Class
Abstract class is any class with an abstract method
Must declare the class with the keyword abstract:
abstract class MyClass {...}
An abstract class is incomplete
It has “missing” method bodies
You cannot instantiate (create a new instance of) an
abstract class
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Abstract Class
You can extend (subclass) an abstract class
If the subclass defines all the inherited abstract
methods, it is “complete” and can be instantiated
If the subclass does not define all the inherited
abstract methods, it too must be abstract
You can declare a class to be abstract even if it does not
contain any abstract methods
This prevents the class from being instantiated
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Why have abstract classes?
Suppose you wanted to create a class Shape, with
subclasses Oval, Rectangle, Triangle, Hexagon, etc.
You don’t want to allow creation of a “Shape”
Only particular shapes make sense, not generic ones
If Shape is abstract, you can’t create a new Shape
You can create a new Oval, a new Rectangle, etc.
Abstract classes are good for defining a general category
containing specific, “concrete” classes
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
An example abstract class
public abstract class Animal {
abstract int eat();
abstract void breathe();
}
This class cannot be instantiated
Any non-abstract subclass of Animal must provide the
eat() and breathe() methods
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Interfaces
An interface declares methods but does not supply bodies
for them
interface KeyListener {
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
public void keyTyped(KeyEvent e);
}
All the methods are implicitly public and abstract
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Interfaces
You cannot instantiate an interface
An interface is like a very abstract class—none of its methods
are defined
An interface may also contain constants (must be
final variables)
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Designing interfaces
Most of the time, you will use Sun-supplied Java
interfaces
Create a custom interface if you want classes of various
types to all have a certain set of capabilities
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Implementing an Interface
You extend a class, but you implement an interface
A class can only extend (subclass) one other class, but it
can implement as many interfaces as one needs
Example:
class MyListener
implements KeyListener, ActionListener { … }
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Implementing an Interface
To implement an interface, you are promising to
define all the methods that were declared in the
interface
Example:
class MyKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {...};
public void keyReleased(KeyEvent e) {...};
public void keyTyped(KeyEvent e) {...};
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Exceptions
An exception is an error condition that changes the
normal flow of control in a program
Exceptions in Java separate error handling from main
business logic
Java has a uniform approach for handling all
synchronous errors
From very unusual (e.g. out of memory)
To more common ones your program should check itself
(e.g. index out of bounds)
From Java run-time system errors (e.g., divide by zero)
To errors that programmers detect and raise deliberately
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Exception Flow of Control
Exceptions break the normal flow of control.
When an exception occurs, the statement that would
normally execute next is not executed.
What happens instead depends on:
whether the exception is caught,
where it is caught,
what statements are executed in the ‘catch block’,
and whether you have a ‘finally block’.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
How to Handle an Exception
1. Prevent the exception from happening (careful
coding)
2. Catch it in the method in which it occurs, and
either
a. Fix the problem and resume normal execution
b. Rethrow it
c. Throw a different exception
3. Declare that the method throws the exception
4. With 1. and 2.a. the caller never knows there was
an error.
5. With 2.b., 2.c., and 3., if the caller does not handle
the exception, the program will terminate and
display a stack trace
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Catching an Exception
try { // statement that could throw an exception
}
catch (<exception type> e) {
// statements that handle the exception
}
catch (<exception type> e) { //e higher in hierarchy
// statements that handle the exception
}
finally {
// release resources
}
//other statements
Note: At most one catch block executes
finally block always executes once,
whether there’s an error or not
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Catch Processing
When an exception occurs, the nested try/catch
statements are searched for a catch parameter
matching the exception class
A parameter is said to match the exception if it:
1. is the same class as the exception; or
2. is a superclass of the exception; or
3. if the parameter is an interface, the
exception class implements the interface.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Catch Processing
The first try/catch statement that has a
parameter that matches the exception has its
catch statement executed.
After the catch statement executes, execution
resumes with the finally statement
After the finally statement is executed, flow
continues with statements after the
try/catch/finally block.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Exception Example
print("now");
try
{ print("is ");
throw new MyException();
print("a ");
}
catch(MyException e)
{ print("the ");
finally { print("time "); }
}
print(”for all good folks\n");
Prints "now is the time for all good folks".
Note: exceptions don't have to be used only for error handling
But any other use is likely to result in code that's hard to
understand.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Exceptions in Java
Exception handling is required for all read/write
methods and for many other system methods
If you use one of Java's built in class methods and it
throws an exception, you must catch it (i.e., surround it
in a try/catch block) or rethrow it, or you will get a
compile time error
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Exceptions in Java
Example:
char ch;
try {ch = (char_System.in.read();}
catch(IOException e) {
System.err.println(e);
return;
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Design <-> Code
Handouts:
UML design for TicTacToe
Java files for implementation
Groups of 2: How does the code reflect the UML design?
What is missing from the UML?
What code consists of interfaces and exception
handling?
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Assignments
Tuesday – Check-off Lab #2
Homework: Chapter 5. Exercises 5.3, 5.4
Programming: Interfaces
Due Friday, Feb. 21
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu