SlideShare a Scribd company logo
Interfaces
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 with it?
 The implementation details are irrelevant to
using the class
 you don’t have to know how things are done
inside the class
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
public interface Doable
{
public void doThis();
public int doThat();
public void doThis2 (float value, char ch);
public boolean doTheOther (int num);
}
interface is a reserved word
None of the methods in
an interface are given
a definition (body)
A semicolon immediately
follows each method header
Interfaces
 An interface cannot be instantiated
 Methods in an interface have public visibility by
default
 A class formally implements an interface by:
 stating so in the class header
 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 ()
{
// whatever
}
// etc.
}
implements is a
reserved word
Each method listed
in Doable is
given a definition
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
 When a class implements an interface, it
gains access to all its constants
Comparison with Inheritance
 Interfaces don’t define any method actions…
you need to fill in all the details
 It essentially just gives a basic collection of
method names
 Not a strict hierarchy
 Important: can implement several Interfaces
 Can use this to “fake” multiple inheritance
Interfaces
 A class can implement multiple interfaces
 The class must implement all methods in all
interfaces listed in the header
class ManyThings implements
interface1, interface2
{
// all methods of both interfaces
}
Example:The Comparable Interface
 Any class can implement Comparable to
provide a mechanism for comparing objects
of that type
 Specifically, implementing Comparable
means that you need a method CompareTo
if (obj1.compareTo(obj2) < 0)
System.out.println ("obj1 is less
than obj2");
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
Requiring Interfaces
 Interface names can be used like class
names in the parameters passed to a method
public boolean isLess(Comparable a,
Comparable b)
{
return a.compareTo(b) < 0;
}
 Any class that “implements Comparable”
can be used for the arguments to this method
Interfaces in UML
 Interfaces are easy to spot in class diagrams
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
Built-in Interfaces
 The Java standard library includes lots more
built-in interfaces
 they are listed in the API with the classes
 Examples:
 Clonable – implements a clone() method
 Formattable – can be formatted with printf
The Iterator Interface
 As we discussed in Chapter 5, an iterator is an object that
provides a means of processing a collection of objects one at a
time
 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
 Once established, the for-each version of the
for loop can be used to process the items in
the iterator
Collections
 Collection is a general interface for any
type that can store multiple values
 Any object c that implements Collections
has these methods
 c.add(e)
 c.remove(e)
 c.size()
Collection Sub-Interfaces
 Interfaces that are derived from Collection
 Set: unordered, can’t add the same object
twice
 List: ordered, adds new methods
 get(i): get the ith element
 set(i,e): set the ith element to e
Collection Implementations
 Also in the standard library: many good
implementations of these interfaces
 List: ArrayList, Stack, LinkedList
 Sets: HashSet, TreeSet
 Each implementation has some differences…
suitable for particular problems
 e.g. additional methods, different type restrictions,
etc.
Example: Pairs
 A class to represent a pair (x,y) of values
 Both values represented with Double
class Pair
{
Double x, y;
public Pair(double x, double y)
{
this.x = new Double(x);
this.y = new Double(y);
}
}
Example: Pairs
 Want to be able to compare..
class Pair implements Comparable<Pair>
{
….
public int compareTo(Pair other)
{
if(this.x.equals(other.x))
return this.y.compareTo(other.y);
else
return this.x.compareTo(other.x);
}
}
Implementing versus Inheriting
 Implementing an Interface is very similar to
inheriting a class
class MyClass implements MyInterface {…}
 Takes everything from MyInterface and puts it in
MyClass
 Except all the methods must be implemented
here
 No previous implementations to fall back on
Interfaces vs. Abstract Classes
 Similarities
 neither can be instantiated
 both can be used as the starting point for a class
 Differences
 A class can contain implementations of methods
 A class can implement many interfaces, but only
one class
Comparison
 In order of “abstractness”:
 Interface
 no method implementations
 can’t be instantiated
 Abstract class
 some method implementations
 can’t be instantiated
 Non-abstract class
 all methods implemented
 can be instantiated

More Related Content

Similar to Interfaces .ppt

06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
 
Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5dplunkett
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Test Presentation
Test PresentationTest Presentation
Test Presentation
Robert Rico Edited
 
C# program structure
C# program structureC# program structure
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
Raffi Khatchadourian
 
Interface
InterfaceInterface
Interface
Shantilal Bhayal
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
G C Reddy Technologies
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Raffi Khatchadourian
 

Similar to Interfaces .ppt (20)

06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Test Presentation
Test PresentationTest Presentation
Test Presentation
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
C# program structure
C# program structureC# program structure
C# program structure
 
Java interface
Java interfaceJava interface
Java interface
 
Interfaces
InterfacesInterfaces
Interfaces
 
Java 06
Java 06Java 06
Java 06
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Collections
CollectionsCollections
Collections
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Interface
InterfaceInterface
Interface
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 

Recently uploaded

2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 

Recently uploaded (20)

2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 

Interfaces .ppt

  • 2. 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 with it?  The implementation details are irrelevant to using the class  you don’t have to know how things are done inside the class
  • 3. 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
  • 4. Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header
  • 5. Interfaces  An interface cannot be instantiated  Methods in an interface have public visibility by default  A class formally implements an interface by:  stating so in the class header  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
  • 6. Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition
  • 7. 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  When a class implements an interface, it gains access to all its constants
  • 8. Comparison with Inheritance  Interfaces don’t define any method actions… you need to fill in all the details  It essentially just gives a basic collection of method names  Not a strict hierarchy  Important: can implement several Interfaces  Can use this to “fake” multiple inheritance
  • 9. Interfaces  A class can implement multiple interfaces  The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }
  • 10. Example:The Comparable Interface  Any class can implement Comparable to provide a mechanism for comparing objects of that type  Specifically, implementing Comparable means that you need a method CompareTo if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is less than obj2");
  • 11. 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
  • 12. Requiring Interfaces  Interface names can be used like class names in the parameters passed to a method public boolean isLess(Comparable a, Comparable b) { return a.compareTo(b) < 0; }  Any class that “implements Comparable” can be used for the arguments to this method
  • 13. Interfaces in UML  Interfaces are easy to spot in class diagrams
  • 14. 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
  • 15. Built-in Interfaces  The Java standard library includes lots more built-in interfaces  they are listed in the API with the classes  Examples:  Clonable – implements a clone() method  Formattable – can be formatted with printf
  • 16. The Iterator Interface  As we discussed in Chapter 5, an iterator is an object that provides a means of processing a collection of objects one at a time  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
  • 17. 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  Once established, the for-each version of the for loop can be used to process the items in the iterator
  • 18. Collections  Collection is a general interface for any type that can store multiple values  Any object c that implements Collections has these methods  c.add(e)  c.remove(e)  c.size()
  • 19. Collection Sub-Interfaces  Interfaces that are derived from Collection  Set: unordered, can’t add the same object twice  List: ordered, adds new methods  get(i): get the ith element  set(i,e): set the ith element to e
  • 20. Collection Implementations  Also in the standard library: many good implementations of these interfaces  List: ArrayList, Stack, LinkedList  Sets: HashSet, TreeSet  Each implementation has some differences… suitable for particular problems  e.g. additional methods, different type restrictions, etc.
  • 21. Example: Pairs  A class to represent a pair (x,y) of values  Both values represented with Double class Pair { Double x, y; public Pair(double x, double y) { this.x = new Double(x); this.y = new Double(y); } }
  • 22. Example: Pairs  Want to be able to compare.. class Pair implements Comparable<Pair> { …. public int compareTo(Pair other) { if(this.x.equals(other.x)) return this.y.compareTo(other.y); else return this.x.compareTo(other.x); } }
  • 23. Implementing versus Inheriting  Implementing an Interface is very similar to inheriting a class class MyClass implements MyInterface {…}  Takes everything from MyInterface and puts it in MyClass  Except all the methods must be implemented here  No previous implementations to fall back on
  • 24. Interfaces vs. Abstract Classes  Similarities  neither can be instantiated  both can be used as the starting point for a class  Differences  A class can contain implementations of methods  A class can implement many interfaces, but only one class
  • 25. Comparison  In order of “abstractness”:  Interface  no method implementations  can’t be instantiated  Abstract class  some method implementations  can’t be instantiated  Non-abstract class  all methods implemented  can be instantiated