Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Java CS 105. 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:

Similar presentations


Presentation on theme: "Data Structures and Java CS 105. 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:"— Presentation transcript:

1 Data Structures and Java CS 105

2 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 2 Review of Java Topics The following Java features play an important role when implementing data structures in Java Interfaces Exceptions The Java class hierarchy and the Object class

3 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 3 Interfaces An interface indicates the method signatures for the operations of a data structure An implementation of the data structure is a Java class that implements this interface to enforce the definition of all methods There can be multiple implementations of the same interface/data structure

4 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 4 Example: Dictionary Dictionary SimpleDictionaryBetterDictionary

5 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 5 public interface Dictionary { public void addWord( String word, String definition ); public void getDefinition( String word ); } public class SimpleDictionary implements Dictionary { // define addWord and getDefinition } public class BetterDictionary implements Dictionary { // another implementation // define addWord and getDefinition }

6 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 6 Exceptions Some operations of data structures may be invalid in certain situations One option: handle the error within that method by printing an error message Can be annoying since the user of the method may get the message interspersed with other output Better alternative: throw exceptions so that the user of the method can decide how to deal with the error

7 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 7 Exceptions in Java Exceptions are handled using a try-catch statement Exceptions are thrown from the method that could cause the exception What needs to be done Define a class that extends Exception (the class may be empty) In the method declaration, include a throws clause In the method body, include a throw statement where the exception occurs

8 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 8 Example public class SimpleDictionary implements Dictionary { //… public void addWord( String word, String definition ) throws DuplicateWordException { if ( getDefinition( word ) != null ) throw new DuplicateWordException(); // code to add dictionary entry here… } // … } public class DuplicateWordException extends Exception { // this class could be empty }

9 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 9 Example Dictionary d = new SimpleDictionary(); try { d.addWord( “bat”, “mammal with wings” ); d.addWord( “cat”, “animal with whiskers” ); d.addWord( “bat”, “equipment used in baseball” ); d.addWord( “elephant”, “a large mammal” ); } catch( DuplicateWordException e ) { System.out.println( “Duplicate Word Error” ); } An exception will be thrown on this call

10 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 10 More on Exceptions Different kinds of exceptions can be handled using a try-catch chain Can have a more elaborate exception class by defining exception/error details inside the class; for example: error message additional data about the error (in the example, the word that causes the duplicate to occur can be stored in the DuplicateWordException class)

11 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 11 RuntimeException Make the exception class extend RuntimeException instead of Exception whenever you do not want to require that the exception be caught The user of the method may or may not use a try-catch statement (try-catch is required for Exceptions) If not within a try-catch, the program aborts

12 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 12 Interfaces and Exceptions In general, when a class implements an interface, the throws clause should be present in both the interface and the class that implements it However, an implementing class can throw additional exceptions as long as they are runtime exceptions

13 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 13 Inheritance hierarchy The extends keyword/feature in Java creates an inheritance hierarchy If a class does not extend another class, it implicitly extends Object, a built-in class in Java This means all classes are subclasses of Object Variables of type Object can refer to an instance of any class

14 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 14 The Object class and data structures When establishing interfaces for data structures, it might be better to use the Object class instead of particular types Example for the Queue interface: public void enqueue( Object o ); public Object dequeue(); instead of public void enqueue( String s ); public String dequeue(); Will need to cast when retrieving an object from the data structure: String s = ( String ) q.dequeue(); Supports Strings and other types of objects

15 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 15 Summary Interfaces allow us to standardize method signatures and to have multiple implementations in a uniform manner Exceptions allow us to elegantly handle errors/unexpected situations The Object class allows our data structures to contain instances of any type/class


Download ppt "Data Structures and Java CS 105. 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:"

Similar presentations


Ads by Google