EE2E1. JAVA Programming Lecture 4 Interfaces. Contents Interfaces – introduction Interfaces – introduction Example – generic sorting Example – generic.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Interfaces and polymorphism Chapter 9. Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum.
Interfaces and Polymorphism Lesson - 8. Objectives Interfaces Supertype and subtype references Polymorphism Inner classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
What is an Interface?? An interface is a ‘container’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Interfaces and Polymorphism.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Chapter 9  Interfaces and Polymorphism 1 Chapter 9 Interfaces and Polymorphism.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
Object Oriented Programming Classes and Objects Dr. Mike Spann
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Interfaces and Polymorphism 9.1 Using Interfaces for Code Reuse Defining an Interface implementing an interface 9.2 Converting between Class.
What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Object Oriented Programming Classes and Objects Dr. Mike Spann
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
EE2E1. JAVA Programming Lecture 3 Inheritance. Contents Base classes and derived classes Base classes and derived classes Protected scope Protected scope.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Fall 2006Adapted from Java Concepts Companion Slides1 Interfaces and Polymorphism Advanced Programming ICOM 4015 Lecture 10 Reading: Java Concepts Chapter.
CHAPTER 9 INTERFACES AND POLYMORPHISM Chapter Goals: –To learn about interfaces –To be able to convert between supertype and subtype references –To understand.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
CHAPTER 9 INTERFACES AND POLYMORPHISM. CHAPTER GOALS To learn about interfaces To be able to convert between supertype and subtype references To understand.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
Interfaces and Inner Classes
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 7 User-Defined Methods.
Chapter Goals To be able to declare and use interface types
Inheritance and Polymorphism
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Interfaces and Polymorphism
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Chapter 11 – Interfaces and Polymorphism
Packages, Interfaces & Exception Handling
Lecture 2 Objects and Classes
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Interface.
Chapter 11 Interfaces and Polymorphism
Object Oriented Programming
Interfaces.
Chapter 12 Abstract Classes and Interfaces
Arrays .
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Interfaces.
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Object Oriented Programming
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

EE2E1. JAVA Programming Lecture 4 Interfaces

Contents Interfaces – introduction Interfaces – introduction Example – generic sorting Example – generic sorting Generic sorting – use of interfaces Generic sorting – use of interfaces Properties of interfaces Properties of interfaces Example use of interfaces Example use of interfaces  Callback functions

Interfaces - introduction An interface is a construct consisting of unimplemented public methods (rather like abstract classes) An interface is a construct consisting of unimplemented public methods (rather like abstract classes) A class which implements an interface provides implementations of all of the methods declared in the interface A class which implements an interface provides implementations of all of the methods declared in the interface Interfaces are widely used in Java and enable multiple inheritance to be avoided Interfaces are widely used in Java and enable multiple inheritance to be avoided

Example – generic sorting Suppose we want write a sort program that is able to sort many kinds of objects Suppose we want write a sort program that is able to sort many kinds of objects We can do this using inheritance and an abstract base class Sortable with method compareTo() We can do this using inheritance and an abstract base class Sortable with method compareTo() We derive what we want to sort from Sortable and polymorphism does the rest We derive what we want to sort from Sortable and polymorphism does the rest For example, we can sort an array of students based on the IDNumber field For example, we can sort an array of students based on the IDNumber field  We need to provide an implementation of compareTo() in the class Student

public abstract class Sortable { public abstract int compareTo(Sortable b); }

compareTo(Sortable b) returns –1,0 or 1 depending on whether some chosen instance field f of a Sortable object is such that : compareTo(Sortable b) returns –1,0 or 1 depending on whether some chosen instance field f of a Sortable object is such that :  this.f<b.f  this.f==b.f  this.f>b.f

public class ArrayAlg { public static void shellSort(Sortable[] a) { int n = a.length; int incr = n / 2; while (incr >= 1) { for (int i = incr; i < n; i++) { Sortable temp = a[i]; int j = i; while (j >= incr && temp.compareTo(a[j - incr]) < 0) { a[j] = a[j - incr]; j -= incr; } a[j] = temp; } incr /= 2; }

public class Student extends Sortable {.. public int compareTo(Sortable b) { Student s=(Student) b; Student s=(Student) b; if (idNnumber<s.idNumber) return –1; if (idNnumber<s.idNumber) return –1; if (idNumber>s.idNumber) return 1; if (idNumber>s.idNumber) return 1; return 0; return 0;} private String name; private int idNumber; private String address; }

public class StudentSortTest { public static void main(String[] args) { Student[] s=new Student[3]; s[0]=new Student(“John Smith”, 3429, “21 Bristol Rd”); s[1]=new Student(“Alan Jones”, 5395, “30 Bournbrook Rd”); s[2]=new Student(“Peter Brown”, 1643, “20 Bristol Rd”); ArrayAlg.shellSort(s);// Sorts on id. number }

Generic sorting – use of interfaces Suppose we wanted to sort objects of the class Square on the basis of length of side Suppose we wanted to sort objects of the class Square on the basis of length of side Square is already derived from the abstract Shape class Square is already derived from the abstract Shape class Would need to also derive Square from Sortable for generic sorting Would need to also derive Square from Sortable for generic sorting Shape Square Sortable

Java does not allow multiple inheritance (unlike C++) Java does not allow multiple inheritance (unlike C++) The solution is to make the class Sortable an interface and then to make the class Square implement that interface The solution is to make the class Sortable an interface and then to make the class Square implement that interface The interface contains the compareTo() method The interface contains the compareTo() method  This method is public by default Any class implementing the interface must provide an implementation of compareTo() Any class implementing the interface must provide an implementation of compareTo() An interface is similar to an abstract class but there are differences (see later) An interface is similar to an abstract class but there are differences (see later)

public interface Sortable { public int compareTo(Sortable s); }

public class Square extends Shape implements Sortable { public Square(int x, int y, int s){super(x,y); side=s;} public float area() {return side*side;} public int compareTo(Sortable s) { Square sq=(Square) s; Square sq=(Square) s; if (side<sq.side) return –1; if (side<sq.side) return –1; if (side>sq.side) return 1; if (side>sq.side) return 1; return 0; return 0;} private int side; }

public class SquareTest { public static void main(String[] args) { Square[] s=new Square[3]; s[0]=new Square(10,0,0); s[1]=new Square(5,0,0); s[2]=new Square(15,0,0); ArrayAlg.shellSort(s);// Sorts on length of side }

All descendants of a class would automatically implement any interfaces implemented by the class All descendants of a class would automatically implement any interfaces implemented by the class For example, if Student implemented the Sortable interface, then so would UndergradStudent and PostGradStudent For example, if Student implemented the Sortable interface, then so would UndergradStudent and PostGradStudent  UndergradStudent and PostGradStudent could then be sorted

We could also have used the interface Comparable defined in the standard package (Java.lang) We could also have used the interface Comparable defined in the standard package (Java.lang)  Provides a compareTo() method public interface Comparable { int compareTo(Object b); } Java provides many interfaces in its standard packages Java provides many interfaces in its standard packages

Properties of interfaces Interfaces are similar to abstract classes in that they cannot be instantiated Interfaces are similar to abstract classes in that they cannot be instantiated Sortable s = new Sortable(); // Error! Sortable is an interface Sortable s = new Square(…); // OK Square sq = new Square(…); if (s.compareTo(sq)>0) {…} However, an interface can be the data type of an object reference However, an interface can be the data type of an object reference

A class can only have a single parent, but it can have multiple interfaces A class can only have a single parent, but it can have multiple interfaces  Multiple inheritance can lead to ambiguities in inheriting object instance fields whereas  Interfaces don’t have (non-static) instance fields – they are merely a set of methods that the object implementing the interface methods must define

public interface Drawable { int draw(); } public class Square extends Shape implements Sortable, Drawable { public int compareTo(Sortable s) {…} public int draw() {…} }

Example a Measurable interface Suppose we have a DataSet class which computes simple statistics of numbers read from an input stream Suppose we have a DataSet class which computes simple statistics of numbers read from an input stream  For example, the average and maximum DataSet average maximum Input stream

public class DataSet { public DataSet() { sum=0.0; maximum=0.0; count=0; } public void add(double x) {sum+=x; if (count==0 || maximum<x) maximum=x;count++;} public double getAverage() { if (count==0) return 0; else return sum/count; else return sum/count;} public double getMaximum() { return maximum; } private double sum, maximum; private int count; }

Clearly we would have to modify the DataSet class if we wanted to get the average of a set of bank account balances or to find the coin with the highest value amongst a set Clearly we would have to modify the DataSet class if we wanted to get the average of a set of bank account balances or to find the coin with the highest value amongst a set  DataSet is not re-useable as it stands  However, if all classes that DataSet objects operate on implement a Measurable interface, then the class becomes more flexible

Simple Measurable interface Simple Measurable interface Thus getMeasure() for BankAccount objects return the balance and for Coin objects returns the coin value Thus getMeasure() for BankAccount objects return the balance and for Coin objects returns the coin value public interface Measurable { double getMeasure(); }

public class BankAccount implements Measurable { public BankAccount(double b) {balance=b;} public double getMeasure() { return balance; } private double balance; } public class Coin implements Measurable { public Coin(double c) {value=c;} public double getMeasure() { return value; } private double value; }

The Measurable interface expresses the commonality amongst objects The Measurable interface expresses the commonality amongst objects  The fact that each measurable objects can return a value relating to its size DataSet objects can then be used to analyse collections of objects of any class implementing this interface with minor modifications to the code DataSet objects can then be used to analyse collections of objects of any class implementing this interface with minor modifications to the code

public class DataSet { public DataSet() { sum=0.0; count=0; } public void add(Measurable x) {sum+=x.getMeasure(); if (count==0 || maximum.getMeasure()<x.getMeasure())maximum=x;count++;} public double getAverage() { if (count==0) return 0; else return sum/count; else return sum/count;} public double getMaximum() { return maximum.getMeasure(); } private Measurable maximum; private double sum; private int count; }

We can now use these classes as follows: We can now use these classes as follows: DataSet d=new DataSet(); Coin c1=new Coin(10); Coin c2=new Coin(20); d.add(c1); d.add(c2); double maxCoin=d.getMaximum(); System.out.println("coin max= " + maxCoin);

Interfaces and callbacks The DataSet class is useful as a re-usable class but is still limited The DataSet class is useful as a re-usable class but is still limited  The Measurable interface can only be implemented by user defined classes  We can’t, for example, find the maximum of a set of Rectangle objects as Rectangle is a pre- defined class  We can only measure an object in one way. For example, in the case of BankAccount objects, we can only measure it in terms of the balance

The solution is to delegate the measuring to a separate class rather than being the responsibility of the objects we are measuring The solution is to delegate the measuring to a separate class rather than being the responsibility of the objects we are measuring  We can create a separate Measurer interface and implement a measure() function in objects implementing this interface public interface Measurer { double measure(Object anObject); }

We update the DataSet class as follows : We update the DataSet class as follows : public DataSet { public DataSet(Measurer m) {measurer=m;} public void add(Object x) {sum=sum+measurer.measure(x); if (count==0 || measurer.measure(maximum)<measurer.measure(x)) measurer.measure(maximum)<measurer.measure(x))maximum=x;count++;} …….. …….. private Measurer measurer; }

public class RectangleMeasurer implements Measurer { public double measure(Object anObject) { Rectangle r=(Rectangle) anObject; double area=r.getWidth()*r.getHeight(); return area; }}

of an object implementing the Measurer interface when it needs to measure an object (such as checking a bank balance) A DataSet object makes a callback to the measure() method of an object implementing the Measurer interface when it needs to measure an object (such as checking a bank balance)  This is in contrast to calling the getMeasure() method of an object implementing the Measurable interface We are now free to design any kind of measures on an object of any class We are now free to design any kind of measures on an object of any class  For example, we can measure Rectangle objects by area  We require a RectangleMeasurer class which implements the Measurer interface

We can now use these classes as follows: We can now use these classes as follows: Measurer m=new RectangleMeasurer(); DataSet data=new DataSet(m); // Add rectangles to the data set data.add(new Rectangle(5,10,20,30)); // Get maximum double max=data.getMaximum();

We have flexibility over our implementation of RectangleMeasurer so that any feature can be measured We have flexibility over our implementation of RectangleMeasurer so that any feature can be measured  Or even defining several measurer classes to measure different features

And finally So far we have covered fundamental issues relating to object orientation including classes, inheritance and polymorphism and interfaces So far we have covered fundamental issues relating to object orientation including classes, inheritance and polymorphism and interfaces In the next set of lectures we will cover more advanced features of java In the next set of lectures we will cover more advanced features of java  Graphics and Swing  Files and streams  Multi-threading  Networks