Presentation is loading. Please wait.

Presentation is loading. Please wait.

MCS 270 Spring 2014 Object-Oriented Software Development.

Similar presentations


Presentation on theme: "MCS 270 Spring 2014 Object-Oriented Software Development."— Presentation transcript:

1 MCS 270 Spring 2014 Object-Oriented Software Development

2 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Today’s schedule Homework: Due today Java Review: Abstract Classes Interfaces Exceptions Example: Tic Tac Toe from UML MCS 270 Object-Oriented Software Development

3 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

4 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

5 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

6 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

7 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

8 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

9 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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) MCS 270 Object-Oriented Software Development

10 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

11 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 { … } MCS 270 Object-Oriented Software Development

12 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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) {...}; } Implementing an Interface MCS 270 Object-Oriented Software Development

13 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Exceptions MCS 270 Object-Oriented Software Development 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

14 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Exception Flow of Control MCS 270 Object-Oriented Software Development 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’.

15 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu How to Handle an Exception MCS 270 Object-Oriented Software Development 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

16 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Catching an Exception MCS 270 Object-Oriented Software Development try { // statement that could throw an exception } catch ( e) { // statements that handle the exception } catch ( 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

17 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Catch Processing MCS 270 Object-Oriented Software Development 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.

18 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Catch Processing MCS 270 Object-Oriented Software Development 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.

19 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Exception Example MCS 270 Object-Oriented Software Development 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.

20 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Exceptions in Java MCS 270 Object-Oriented Software Development 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

21 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Exceptions in Java MCS 270 Object-Oriented Software Development Example: char ch; try {ch = (char_System.in.read();} catch(IOException e) { System.err.println(e); return; }

22 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Design Code MCS 270 Object-Oriented Software Development 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?

23 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Assignments Tuesday – Check-off Lab #2 Homework: Chapter 5. Exercises 5.3, 5.4 Programming: Interfaces Due Friday, Feb. 21 MCS 270 Object-Oriented Software Development


Download ppt "MCS 270 Spring 2014 Object-Oriented Software Development."

Similar presentations


Ads by Google