CSE 501N Fall ‘09 13: Interfaces and Multiple Representation

Slides:



Advertisements
Similar presentations
Interfaces A Java interface is a collection
Advertisements

Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
Inheritance Inheritance Reserved word protected Reserved word super
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Multiple Choice Solutions True/False a c b e d   T F.
Object Oriented Design and UML
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Polymorphi sm. 2 Abstract Classes Java allows abstract classes – use the modifier abstract on a class header to declare an abstract class abstract class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Introduction to Java Java Translation Program Structure
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
CSE 1201 Object Oriented Programming Abstract Classes and Interfaces.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Lecture 5:Interfaces and Abstract Classes
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Polymorphism.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Lecture 12 Inheritance.
Interfaces.
03/10/14 Inheritance-2.
ADT’s, Collections/Generics and Iterators
Chapter 5: Enhancing Classes
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
CMSC 202 Interfaces.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Slides by Steve Armstrong LeTourneau University Longview, TX
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Chapter 5: Enhancing Classes
Abstract Classes An abstract class is a kind of ghost class. It can pass along methods and variables but it can’t ever be instantiated itself. We can.
Overriding Methods & Class Hierarchies
CMSC 202 Interfaces.
Chapter 8 Inheritance.
Abstract Classes and Interfaces
Chapter 8 Inheritance Part 2.
2009 Test Key.
Object-Oriented Design Part 2
Presentation transcript:

CSE 501N Fall ‘09 13: Interfaces and Multiple Representation 15 October 2009 Nick Leidenfrost

Lecture Outline Lab 4 questions? Interfaces Multiple Representation

Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish a set of methods that a class will implement

Interfaces interface is a reserved word None of the methods in an interface are given a definition (body) public interface Doable { public void doThis(); public int doThat(); public void doThisToo (float value, char ch); public boolean doTheOther (int num); } A semicolon immediately follows each method header [ Eclipse Example: Creation ]

Doable aDoable = new CanDo(); Interfaces An interface cannot be instantiated However, we can have variables with interface types: Doable aDoable = new CanDo(); Methods in an interface have public visibility by default If you omit an access modifier, public is used, not default (package) protected default, protected, and private modifiers not permitted Why does it make sense for an interface only to offer public methods?

Interfaces A class formally implements an interface by: Stating so in the class declaration header Using the implements keyword providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // etc. implements is a reserved word Each method listed in Doable is given a definition [ Eclipse Example: Implementing ]

Interfaces A class that implements an interface can implement other methods as well In addition to (or instead of) abstract methods, an interface can contain constants Any variables declared inside an interface are, by default, static final [ Eclipse Example ] When a class implements an interface, it gains access to all its constants

Interfaces A class can implement multiple interfaces The interfaces are listed in the implements clause, delimited by commas The class must implement all methods in all interfaces listed in the header class ManyThings implements Interface1, Interface2 { // all methods of both interfaces }

Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains one abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is less than obj2"); The value returned from compareTo should be negative if obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer designs a class that implements the Comparable interface, it should follow this intent

The Comparable Interface It's up to the programmer to determine what makes one object less than another For example, you may define the compareTo method of an Employee class to order employees by name (alphabetically) or by employee number The implementation of the method can be as straightforward or as complex as needed for the situation

The Iterator Interface An iterator is created formally by implementing the Iterator interface, which contains three methods The hasNext method returns a boolean result – true if there are items left to process The next method returns the next object in the iteration The remove method removes the object most recently returned by the next method

The Iterator Interface By implementing the Iterator interface, a class formally establishes that objects of that type are iterators The programmer must decide how best to implement the iterator functions It depends on the data (structure) we are iterating over [ Java Library: Iterable ] Once established, the for-each version of the for loop can be used to process the items in the iterator

Interfaces You could write a class that implements certain methods (such as compareTo) without formally implementing the interface (Comparable) However, formally establishing the relationship between a class and an interface allows Java to deal with an object in certain ways Interfaces are a key aspect of object-oriented design in Java

Multiple Representation There are multiple ways to represent almost any kind of data, even all the way down to the seemingly most primitive. E.g., the number 5 can be represented as 5, V, |||||, five, 5.0, 0101 The choice of representation of numbers has great impact on the efficiency with which computations may be carried out. For example, people today generally use the decimal system If we still used the roman numeral system, imagine how much more difficult it would be to make technological progress!

Multiple Representation When designing software, we too have a choice of representations Suppose we want to model a set For internal representation we can choose Arrays LinkedLists Others? How does this look conceptually?

Multiple Representation

Multiple Representation Use an interface to capture the essential elements that must be common across all representations (Usually which methods must be offered) Have concrete implementations (classes) implement the interface Details of implementation are abstracted away

Example: Processing Data The DataProvider interface abstracts away the details of the implementation of how the data is being provided Network Connection You Application (Data Processor) Data Provider Person at Computer The application can interact with all kinds of data providers uniformly, without needing to know which is providing the data. File

Conclusion Questions Lab 5 Assigned Due Next Thursday Lab sessions now