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?

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
CS 211 Inheritance AAA.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Abstract Data Types and Encapsulation Concepts
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
OOP Languages: Java vs C++
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Abstract and Nested Classes
CSC 212 Object-Oriented Programming and Java Part 1.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming in Java CSCI-2220 Object Oriented Programming.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Chapter FifteenModern Programming Languages1 A Second Look At Java.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Nested Classes CompSci 230 S Software Construction.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Programming in Java: lecture 7
Chapter 5: Enhancing Classes
Inheritance and Polymorphism
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Nested class.
Comp 249 Programming Methodology
Interfaces and Inner Classes
Lecture 4: Interface Design
Interfaces.
Interfaces and Inner Classes
Interfaces.
Inner Classes 11-May-19.
Lecture 10 Concepts of Programming Languages
Inner Classes 18-May-19.
Presentation transcript:

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?

Definition from Type Theory  An interface is a “type”  A type is a collection of method specifications A “contract” A “contract” The methods a user can expect to call upon The methods a user can expect to call upon  That’s it!

Separating Interface and Implementation  Why is it a Good Thing?  How is it done?  Example: Figures 1-5 in Jan00

Separation Anxiety  To fully separate interface from implementation, we separate the contract from the class(es) that implement(s) it!

Java Interfaces  Similar to a class definition  Used mainly for function prototypes Abstract method declarations Abstract method declarations Public and abstract by default! Public and abstract by default!  Can also include: constants (static finals) constants (static finals) Nested classes and interfaces (covered later) Nested classes and interfaces (covered later)  Example: Next slide, Figure 6

interface Stack { void push(Object o) throws StackException; Object pop() throws StackException; Object top() throws StackException; int size(); }

class FixedStack implements Stack { // implementation unchanged // from Figure 1 … }

Advantage of Interfaces (over inheritance)  Any class that implements the interface (i.e., provides the contract’s functionality) can be used  Not constrained by subclassing

Implementation Inheritance  What happens with extends  The subclass inherits implementation: Data and method bodies Data and method bodies You’re stuck with a particular implementation You’re stuck with a particular implementation

Interface Inheritance  A commitment to implement a contract  No implementation is inherited  Disadvantage: No code sharing No code sharing  Advantage: No code commitment No code commitment Freedom to implement any way you want Freedom to implement any way you want

Interfaces vs. Abstract Classes  Use Abstract Classes when there is some implementation to share  In C++, an abstract class with only pure virtual functions and no implementation behaves as an interface

Multiple Inheritance  What kind?  Multiple inheritance of implementation is fraught with complexity Virtual base classes Virtual base classes Dominance rules Dominance rules  Multiple inheritance of interfaces just means you must implement all the methods in the hierarchy And that’s all it means And that’s all it means

class DynamicStack implements Stack, Persistent { // implementation as in Figure 4 // PLUS need to implement read and write }

Interfaces as Capabilities  Implements multiple interfaces  Interface names are often adjectives They describe capabilities They describe capabilities  Example: AbleTest.java

Sub- and Super-Interfaces  An interface can extend another interface  The net result is just the union of all the method specifications interface PersistentStack extends Stack, Persistent {} class DynamicStack implements PersistentStack {…}

Interfaces in the Java Library  Comparable: public int compareTo(Object x)  Like C’s strcmp, returns: Negative, if this < x Negative, if this < x Zero if this.equals(x) Zero if this.equals(x) Positive, if this > x Positive, if this > x  You decide how the ordering works  Used throughout the library

Comparing Fractions compareTo( ) should return ad - bc

Interfaces in the Java Library  Cloneable For copying objects For copying objects Kind of dorky Kind of dorky  Serializable For automatic object storage and retrieval For automatic object storage and retrieval  Collection Basic contract for collections Basic contract for collections

Iterators  A Design Pattern for traversing collections  java.util.Iterator interface: public boolean hasNext( ); public Object next( ); public void remove( );  Collections typically implement Iterator Benefit: Can have multiple iterators simultaneously Benefit: Can have multiple iterators simultaneously Implemented as nested classes Implemented as nested classes

Iterator Example  MySequence.java An expandable array of Object An expandable array of Object  MyIterator implements java.util.Iterator  MySequence.getIterator( ) returns a MyIterator object

Issues with MySequence.java  The class MyIterator has more visibility than it needs Package access (we want private) Package access (we want private) Top-level classes can’t be private (or protected)Top-level classes can’t be private (or protected)  Clients only care about the Iterator interface being implemented They don’t care what type actually does it They don’t care what type actually does it  Solution: nest MyIterator inside MySequence

Nested Classes  Can define classes and interfaces within other classes and interfaces  Two flavors: Static (like nested classes in C++) Static (like nested classes in C++) Non-static (different!) Non-static (different!) Also called “inner classes”Also called “inner classes”  Can also define classes inside of a method “local classes” “local classes”

Static Nested Classes  Just like C++  Just a scoping mechanism  class A { static class B {…} } A.B b = new A.B();

Inner Classes  Objects of inner classes only exist in connection with an instance of their containing class(es)  They have an invisible link to the object of the containing class  They can be declared with any access specifier Usually private Usually private  Used a lot in AWT/Swing

Special Syntax  Refer to variable in outer class: Outer.this.varName  Make an object of inner class: Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); = outer.new Inner(); See page 230 of Core Java

MySequence2.java  MyIterator is an inner class  A MyIterator object has an implicit MySequence object that “owns” it Just like “this” is implicit in non-static methods Just like “this” is implicit in non-static methods Any references to MySequence fields is resolved automatically Any references to MySequence fields is resolved automatically  “data” == “MySequence.this.data”

Local Inner Classes  Defined inside a method Not visible outside the method Not visible outside the method  Can only access final locals in the enclosing method See p. 233 in Core Java See p. 233 in Core Java  Example: MySequence3.java

Anonymous Inner Classes  The name MyIterator is only used in one place Hardly seems worth a separate name! Hardly seems worth a separate name!  Can define an unnamed class “on-the-fly” instead in the return expression in MySequence.getIterator( )  Can have no named constructors  Example: MySequence4.java

Marker Interfaces  Interfaces with no definitions!  Merely to “color” or “tag” a class  Cloneable  Serializable Will cover in I/O section Will cover in I/O section

Cloning  Meant to replace copy constructors For “deep copies” For “deep copies” Somewhat problematic, but widely used! Somewhat problematic, but widely used!  Must Implement Cloneable  Must override Object.clone( ) But make it public! (It’s protected in Object) But make it public! (It’s protected in Object) Call super.clone() first! (To get the right type) Call super.clone() first! (To get the right type)  It is an error to call clone( ) for a class that does not implement Cloneable Throws CloneNotSupportedException Throws CloneNotSupportedException  Example: Figures 7-9

Cloning Policy  1) Support it As just described As just described  2) Let subclasses support it Don’t implement Cloneable Don’t implement Cloneable But provide a protected clone if Object.clone() isn’t deep enough But provide a protected clone if Object.clone() isn’t deep enough  3) Forbid cloning Provide a clone( ) that unconditionally throws CloneNotSupportedException Provide a clone( ) that unconditionally throws CloneNotSupportedException