OOP with Java, David J. Barnes Interfaces1 A set of features offered by a class. Often offered by more than one class. –Iterator : Returned by the iterator.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
CS18000: Problem Solving and Object-Oriented Programming.
Java Review Interface, Casting, Generics, Iterator.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
What is an Interface? 4 Common in everyday life. –Movie theaters, TVs, VCRs, etc. 4 Java interface definitions are simple: –Method headers. –Field definitions.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Lecture 9 Concepts of Programming Languages
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Abstract and Nested Classes
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Understanding class definitions
Types in programming languages1 What are types, and why do we need them?
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces and Inner Classes
Interfaces and Polymorphism CS 162 (Summer 2009).
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CS61B L13 Lists (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object Oriented Programming in Java Habib Rostami Lecture 7.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Interface Java 7 COMP T1.
Software Development Java Classes and Methods
Lecture 9 Concepts of Programming Languages
Understanding class definitions
null, true, and false are also reserved.
Java Programming Language
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Understanding class definitions
Chapter 8 Class Inheritance and Interfaces
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Inheritance Lakshmish Ramaswamy.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

OOP with Java, David J. Barnes Interfaces1 A set of features offered by a class. Often offered by more than one class. –Iterator : Returned by the iterator method of ArrayList and LinkedList. Alternative underlying implementations. –Used externally in an identical way. –What kept separate from how.

OOP with Java, David J. Barnes Interfaces2 What is an Interface? Common in everyday life. –Movie theaters, TVs, VCRs, etc. Java interface definitions are simple: –Method headers. –Field definitions ( public static final ).

OOP with Java, David J. Barnes Interfaces3 An Interface Definition interface VCR { public boolean loadTape(String tapeName); public boolean eject(); public boolean play(); public boolean pause(); public boolean stop(); public boolean ff(); public boolean rew(); public int getStatus(); public static final int PLAYING = 0, PAUSED = 1, STOPPED = 2, FORWARD = 3, BACKWARD = 4, NOTAPE = 5; }

OOP with Java, David J. Barnes Interfaces4 Implementing an Interface class NoFrillsVCR implements VCR {... public boolean eject(){ if(getStatus() != NOTAPE){ setStatus(NOTAPE); return true; } else{ return false; }... }

OOP with Java, David J. Barnes Interfaces5 Interface Fields Field definitions are not repeated in defining classes. Use unqualified in implementing classes. Fields accessible in different ways: –Via the interface name: VCR.STOPPED –Via an implementing class name: NoFrillsVCR.STOPPED –Via an instance reference: cheap.STOPPED

OOP with Java, David J. Barnes Interfaces6 Multiple Implementations One of the main reasons for creating an interface. No limit on the number of implementing classes. Implementations may add further features. –Variable playing speed, for instance. –These are not part of the interface.

OOP with Java, David J. Barnes Interfaces7 Polymorphism A method that knows about a particular interface may interact with an object of any of the implementing classes. It will not know about additional functionality: – e.g., variable playing speed. The instanceof operator, and a cast provide access to class-specific information.

OOP with Java, David J. Barnes Interfaces8 The instanceof Operator class VideoTester { private boolean testVCR(VCR video,String tape){... if(video instanceof UpMarketVCR){ // Test its extra features. UpMarketVCR betterVCR = (UpMarketVCR) video; // Play at twice the normal speed. betterVCR.setSpeed(2); }... }

OOP with Java, David J. Barnes Interfaces9 Interfaces without Methods A collecting point for related values: interface PieceNames { public static int Pawn = 0, Rook = 1, Knight = 2, Bishop = 3, Queen = 4, King = 5; } Similar to enumerated types, but no type checking. int piece = PieceNames.King; int badPiece = piece+1; // Nonsense.

OOP with Java, David J. Barnes Interfaces10 Iterator and Enumeration public interface Iterator { public boolean hasNext(); public Object next() throws NoSuchElementException; public void remove() throws IllegalStateException, UnsupportedOperationException; } public interface Enumeration { public boolean hasMoreElements(); public Object nextElement() throws NoSuchElementException; }

OOP with Java, David J. Barnes Interfaces11 Comparable and Comparator public interface Comparable { public int compareTo(Object o) throws ClassCastException; } public interface Comparator { public int compare(Object o1,Object o2) throws ClassCastException; public boolean equals(Object o); } Instances of implementing classes can be sorted.

OOP with Java, David J. Barnes Interfaces12 FileFilter and FilenameFilter public interface FileFilter { // Should the given path name be accepted? public boolean accept(File pathname); } public interface FilenameFilter { // Should name in directory be accepted? public boolean accept(File directory,String name); } Select acceptable/required files.

OOP with Java, David J. Barnes Interfaces13 Further Interface Issues Empty interface. –Can act as a marker ( Cloneable ). Implementing Multiple Interfaces. –A form of multiple inheritance. –Very common in GUI applications. –Conflict resolution required: Identical method names or field names.

OOP with Java, David J. Barnes Interfaces14 Conflict Resolution Common methods with different argument lists are overloaded. Common methods with identical headers have a single implementation. –This has to be meaningful for both interfaces. Checked exception lists must be reduced to the common subset. Common field names must be qualified.

OOP with Java, David J. Barnes Interfaces15 Serializable An empty interface to support persistence: object serialization. Serializable objects are manipulated via ObjectInputStream and ObjectOutputStream objects. transient fields are not serialized. Externalizable provides finer control.

OOP with Java, David J. Barnes Interfaces16 Review An interface contains only method signatures (headers) and/or fields. Fields are all public static final. Interfaces are usually implemented by multiple classes. Polymorphism allows any implementing instances to be typed as the interface type.

OOP with Java, David J. Barnes Interfaces17 Review (cont.) The instanceof operator allows the dynamic type of an object to be determined. Interfaces without methods are often used to collect related constant values. An empty interface may be used as a marker. There are many standard interfaces, e.g., Iterator.