Interfaces and polymorphism Chapter 9. Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Interfaces and Polymorphism Lesson - 8. Objectives Interfaces Supertype and subtype references Polymorphism Inner classes.
Chapter 9 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Chapter 9  Interfaces and Polymorphism 1 Chapter 9 Interfaces and Polymorphism.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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()
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 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.,
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.
CHAPTER 9 INTERFACES AND POLYMORPHISM. CHAPTER GOALS To learn about interfaces To be able to convert between supertype and subtype references To understand.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 9 Chapter.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 9 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
EE2E1. JAVA Programming Lecture 4 Interfaces. Contents Interfaces – introduction Interfaces – introduction Example – generic sorting Example – generic.
Big Java Chapters 9-10.
Lecture 3 John Woodward.
Chapter Goals To be able to declare and use interface types
Inheritance and Polymorphism
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Methods Attributes Method Modifiers ‘static’
Interfaces and Polymorphism
Chapter 11 – Interfaces and Polymorphism
Chapter Three - Implementing Classes
Chapter 9 Inheritance and Polymorphism
Chapter 11 Interfaces and Polymorphism
Group Status Project Status.
Chapter 14 Abstract Classes and Interfaces
Object Oriented Programming
Presentation transcript:

Interfaces and polymorphism Chapter 9

Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum gpa of a group of students. You want to find the maximum balance of the bank accounts of a bank. You use the same operation to find the maximum. With what we know, we would have to rewrite the method for each. There needs to be a better way.

Using Interfaces for Code Reuse  Interface types makes code more reusable  Interface type declares a set of methods and their signatures.  An interface type is similar to a class  Differences All method in an interface type are abstract  Name  Parameter  Return type  Don’t’ have an implementation All methods are automatically public Does not have instance fields

Using Interfaces for Code Reuse  In Chap. 6, we created a DataSet to find the average and maximum of a set of values (numbers)  What if we want to find the average and maximum of a set of BankAccount values?

Using Interfaces for Code Reuse public class DataSet // Modified for BankAccount objects {... public void add(BankAccount x) { sum = sum + x.getBalance(); if (count == 0 || maximum.getBalance() < x.getBalance()) maximum = x; count++; } public BankAccount getMaximum() { return maximum; } private double sum; private BankAccount maximum; private int count; }

Using Interfaces for Code Reuse  Or suppose we wanted to find the coin with the highest value among a set of coins. We would need to modify the DataSet class again

Using Interfaces for Code Reuse public class DataSet // Modified for Coin objects {... public void add(Coin x) { sum = sum + x.getValue(); if (count == 0 || maximum.getValue() < x.getValue()) maximum = x; count++; } public Coin getMaximum() { return maximum; } private double sum; private Coin maximum; private int count; }

Using Interfaces for Code Reuse  The mechanics of analyzing the data is the same in all cases; details of measurement differ  Classes could agree on a method getMeasure that obtains the measure(or the value) to be used in the analysis

Using Interfaces for Code Reuse  We can implement a single reusable DataSet class whose add method looks like this:  In this case x can be either a bank account or it can be a coin or a gpa.  We need an interface. We will call it Measureable It will declare one method (getMeasure) sum = sum + x.getMeasure(); if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; count++;

Example public interface Measurable { double getMeasure(); } Notice: Type is interface No instance fields No implementation

Use  When we do this we can use the DataSet class for any class that implements the Measurable interface

Using Interfaces for Code Reuse  What is the type of the variable x? x should refer to any class that has a getMeasure method sum = sum + x.getMeasure(); if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; count++;

Using Interfaces for Code Reuse  An interface type is used to specify required operations  When we use the interface, our class must have a method or methods that correspond to each method declared in the interface.  Interface declaration lists all methods (and their signatures) that the interface type requires public interface Measurable { double getMeasure(); }

How to Implement Use implements keyword to indicate that a class implements an interface type We must put the method in the program that implement the interface. A class can implement more than one interface type Class must define all the methods that are required by all the interfaces it implements public class BankAccount implements Measurable { public double getMeasure() { return balance; } // Additional methods and fields }

UML Diagram of Dataset and Related Classes  Interfaces can reduce the coupling between classes  UML notation: Interfaces are tagged with a "stereotype" indicator «interface» A dotted arrow with a triangular tip denotes the "is-a" relationship between a class and an interface A dotted line with an open v-shaped arrow tip denotes the "uses" relationship or dependency  Note that DataSet is decoupled from BankAccount and Coin

UML

Generic DataSet for Measureable Objects public class DataSet {... public void add(Measurable x) { sum = sum + x.getMeasure(); if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; count++; } public Measurable getMaximum() { return maximum; } private double sum; private Measurable maximum; private int count; }

File DataSetTester.java 01: /** 02: This program tests the DataSet class. 03: */ 04: public class DataSetTester 05: { 06: public static void main(String[] args) 07: { 08: DataSet bankData = new DataSet(); 09: 10: bankData.add(new BankAccount(0)); 11: bankData.add(new BankAccount(10000)); 12: bankData.add(new BankAccount(2000)); 13: 14: System.out.println("Average balance = " 15: + bankData.getAverage()); 16: Measurable max = bankData.getMaximum(); 17: System.out.println("Highest balance = " 18: + max.getMeasure());

File DataSetTester.java 19: 20: DataSet coinData = new DataSet(); 21: 22: coinData.add(new Coin(0.25, "quarter")); 23: coinData.add(new Coin(0.1, "dime")); 24: coinData.add(new Coin(0.05, "nickel")); 25: 26: System.out.println("Average coin value = " 27: + coinData.getAverage()); 28: max = coinData.getMaximum(); 29: System.out.println("Highest coin value = " 30: + max.getMeasure()); 31: } 32: }

Output Average balance = Highest balance = Average coin value = Highest coin value = 0.25

Converting Between Class and Interface Types  Interfaces are used to express the commonality between classes  You can convert from a class type to an interface type, provided the class implements the interface BankAccount account = new BankAccount(10000); Measurable x = account; // OK Coin dime = new Coin(0.1, "dime"); Measurable x = dime; // Also OK

Converting Between Class and Interface Types  You can not convert between unrelated types Measurable x = new Rectangle (5,10,20,30); // illegal  Because Rectangle doesn't implement Measurable  Rectangle can’t implement Measurable because it is a system class

Casts  Add coin objects to DataSet  What can you do with it? It's not of type Coin DataSet coinData = new DataSet(); coinData.add(new Coin(0.25, "quarter")); coinData.add(new Coin(0.1, "dime"));... Measurable max = coinData.getMaximum(); // Get the largest coin String name = max.getName(); // ERROR Continued…

Casts  You need a cast to convert from an interface type to a class type  You know it's a coin, but the compiler doesn't. Apply a cast:  If you are wrong and max isn't a coin, the compiler throws an exception Coin maxCoin = (Coin) max; String name = maxCoin.getName();

Casts  Difference with casting numbers: When casting number types you agree to the information loss When casting object types you agree to that risk of causing an exception

Polymorphism  Interface variable holds reference to object of a class that implements the interface Measurable x ; Note that the object to which x refers doesn't have type Measurable ; the type of the object is some class that implements the Measurable interface Continued… x = new BankAccount(10000); x = new Coin(0.1, "dime");

Polymorphism  You can call any of the interface methods:  Which method is called? double m = x.getMeasure();

Polymorphism  Depends on the actual object.  If x refers to a bank account, calls BankAccount.getMeasure  If x refers to a coin, calls Coin.getMeasure  Polymorphism (many shapes): Behavior can vary depending on the actual type of an object Continued…

Polymorphism  Called late binding: resolved at runtime  Different from overloading; overloading is resolved by the compiler (early binding)  Remember – overloading is when you have 2 methods with the same name. The explicit parameter determines which method will be used.

Using Interfaces for Callbacks  Limitations of Measurable interface:  Can add Measurable interface only to classes under your control  Can measure an object in only one way E.g., cannot analyze a set of savings accounts both by bank balance and by interest rate  Callback mechanism: allows a class to call back a specific method when it needs more information

Using Interfaces for Callbacks  Object is the "lowest common denominator" of all classes  In previous DataSet implementation, responsibility of measuring lies with the added objects themselves  Alternative: Hand the object to be measured to a method: public interface Measurer { double measure(Object anObject); }

Using Interfaces for Callbacks  add method asks measurer (and not the added object) to do the measuring public void add(Object x) { sum = sum + measurer.measure(x); if (count == 0 || measurer.measure(maximum) < measurer.measure(x)) maximum = x; count++; }

Using Interfaces for Callbacks  You can define measurers to take on any kind of measurement public class RectangleMeasurer implements Measurer { public double measure(Object anObject) { Rectangle aRectangle = (Rectangle) anObject; double area = aRectangle.getWidth() * aRectangle.getHeight(); return area; } }

Using Interfaces for Callbacks  Must cast from Object to Rectangle  Pass measurer to data set constructor: Rectangle aRectangle = (Rectangle) anObject; Measurer m = new RectangleMeasurer(); DataSet data = new DataSet(m); data.add(new Rectangle(5, 10, 20, 30)); data.add(new Rectangle(10, 20, 30, 40));...

UML  Note that the Rectangle class is decoupled from the Measurer interface

Inner Classes  Trivial class can be defined inside a method public class DataSetTester3 { public static void main(String[] args) { class RectangleMeasurer implements Measurer {... } Measurer m = new RectangleMeasurer(); DataSet data = new DataSet(m);... } } Continued…

Inner Classes  If inner class is defined inside an enclosing class, but outside its methods, it is available to all methods of enclosing class  Compiler turns an inner class into a regular class file: DataSetTester$1$RectangleMeasurer.class

Syntax 11.3: Inner Classes Declared inside a method class OuterClassName { method signature {... class InnerClassName { // methods // fields }... }... } Declared inside the class class OuterClassName { // methods // fields accessSpecifier class InnerClassName { // methods // fields }... } Continued…

Syntax 11.3: Inner Classes Example: public class Tester { public static void main(String[] args) { class RectangleMeasurer implements Measurer {... }... } Purpose: To define an inner class whose scope is restricted to a single method or the methods of a single class

File FileTester3.java 01: import java.awt.Rectangle; 02: 03: /** 04: This program demonstrates the use of a Measurer. 05: */ 06: public class DataSetTester3 07: { 08: public static void main(String[] args) 09: { 10: class RectangleMeasurer implements Measurer 11: { 12: public double measure(Object anObject) 13: { 14: Rectangle aRectangle = (Rectangle) anObject; 15: double area 16: = aRectangle.getWidth() * aRectangle.getHeight(); 17: return area; Continued…

File FileTester3.java 18: } 19: } 20: 21: Measurer m = new RectangleMeasurer(); 22: 23: DataSet data = new DataSet(m); 24: 25: data.add(new Rectangle(5, 10, 20, 30)); 26: data.add(new Rectangle(10, 20, 30, 40)); 27: data.add(new Rectangle(20, 30, 5, 10)); 28: 29: System.out.println("Average area = " + data.getAverage()); 30: Rectangle max = (Rectangle) data.getMaximum(); 31: System.out.println("Maximum area rectangle = " + max); 32: } 33: }

Accessing Surrounding Variables  Local variables that are accessed by an inner-class method must be declared as final  Inner class can access fields of surrounding class that belong to the object that constructed the inner class object  An inner class object created inside a static method can only access static surrounding fields