Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +

Slides:



Advertisements
Similar presentations
Java Programming: Advanced Topics 1 Collections and Utilities.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
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.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
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.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Packages and Libraries M Taimoor Khan
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
CS2210: SW Development Methods Topics: Comparable and Comparator interfaces in JCF Function objects Textbook readings: More from MSD, Chapter 9 Pages
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Today’s lecture Review of chapter 8 Go over examples.
University of Limerick1 Collections The Collection Framework.
Collections Dwight Deugo Nesa Matic
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Java Coding 6 David Davenport Computer Eng. Dept.,
Programming in Java: lecture 7
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding 2 David Davenport Computer Eng. Dept.,
Java Coding Extra David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 David Davenport Computer Eng. Dept.,
Iterator.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Functional interface.
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces + Inner classes

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

SOME JAVA INTERFACES…

Java Interfaces Design by interface gives flexibility The Java API has a lot of interfaces Iterator Comparable Serializable & a multitude for GUI event-handling!

Java’s Iterator Interface boolean hasNext() Object next() void remove() // Scanner class implements Iterator tokens = new Scanner( “To be or not to be”); while ( tokens.hasNext() ) System.out.println( tokens.next() ); To iterate (to go) through a collection, processing each element once and once only

Java’s Iterator Interface boolean hasNext() Object next() void remove() // given an ArrayList, list Iterator x = list.iterator(); while ( x.hasNext() ) System.out.println( x.next() ); Surprisingly, ArrayList does not implement Iterator. It implements Iterable! Iterator iterator()

The Enumeration Interface boolean hasNextElement() E nextElement() // StringTokenizer tokens = new StringTokenizer( “To be or not to be”); while ( tokens.hasMoreElements() ) System.out.println( tokens.nextElement() ); Similar to, but older than Iterator. Has different names & does not have remove method. Examples include StringTokenizer & Vector (which also has Iterator!)

Java’s Comparable Interface int compareTo( Object o) compare this object with o and return negative, zero, positive to indicate respectively! // Assuming x implements Comparable // e.g. String if ( x.compareTo( y) > 0 ) exchange( x, y);

Java’s Serialization Interface Allows Java objects to be converted to/from a stream of bytes! Rather special since you do not need to write any methods, merely Make class implement Serializable Have a default constructor Ensure all properties are Serializable Check the Java API documentation for details and example code.

Interface notes Can’t create objects of interface type, only a type that implements it Interfaces can extend interfaces (but not classes) to form a hierarchy separate from class one Start design with interfaces! Java provides “instanceof” operator to test object type (but use very sparingly) Cloneable is another common interface.

INNER CLASSES…

Inner Classes Nested or Inner classes are defined inside other classes have direct access to outer class can be named or anonymous Named Inner classes Simply define one class inside another! (generates outer$inner.class files)

Inner Classes (cont.) Anonymous Inner classes Create in-line (on the fly!) Useful when only a single object needed & class not otherwise reusable. variation of new statement… className x = new superClass_or_interface() { // the prop’s & methods }