What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

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.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
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.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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. 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.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
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.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Interfaces and Polymorphism 9.1 Using Interfaces for Code Reuse Defining an Interface implementing an interface 9.2 Converting between Class.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
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.,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Topic 4 Inheritance.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
CSE 1201 Object Oriented Programming ArrayList 1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
OOP Basics Classes & Methods (c) IDMS/SQL News
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
EE2E1. JAVA Programming Lecture 4 Interfaces. Contents Interfaces – introduction Interfaces – introduction Example – generic sorting Example – generic.
Modern Programming Tools And Techniques-I
Chapter Goals To be able to declare and use interface types
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Interfaces and Polymorphism
Chapter 11 – Interfaces and Polymorphism
Interfaces and Inheritance
Chapter 11 Interfaces and Polymorphism
Presentation transcript:

What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps() ; //returns String representation of object in all capital letters public abstract String outFirstCap(); //returns String representation of object w/ first char in caps public abstract String outNoCaps(); // returns String representation of object in all lowercase }

What is an Interface FOR?? An interface can be used to FORCE a class to provide certain functionality. Suppose we needed to be certain that the class Phrase provided a way to output data: in all capitals with first letter capitalized in all lower case

public class Phrase implements Capitalizable { String the_data; public Phrase() { // code for this method } public void addWord(String word) { // code for this method } public int howMany() { // code for this method } //Phrase MUST fully define all methods which are in Capitalizable

//the class will NOT compile without these methods!! public String outCaps() { // code for this method } public String outFirstCap(){ // code for this method } public String outNoCaps() { // code for this method } ADDITIONALLY, an object of class Phrase is ALSO an object of type Capitalizable!!!!!!!!!!!

Why might I need to ensure that a class offers the methods listed in Capitalizable?? Suppose we wanted to write a program which creates Sentence objects, Paragraph objects, Word object, and BlockText objects And our program was to store these objects and output them all in capital letters.

Sentence [] sobj; Paragraph [] pobj; Word [] wobj; Blocktext [] bobj; sobj = new Sentence [100]; pobj = new Paragraph [100]; wobj = new Word [100]; bobj = new Blocktext [100]; sobj[0] = new Sentence(); pobj[0] = new Paragraph(); wobj[0] = new Word(); bobj[0] = new BlockText(); ….. // other assignments sobj[15] = new Sentence(); pobj[15] = new Paragraph(); wobj[15] = new Word(); bobj[15] = new BlockText();

for (int index=0; index < 15; index++) System.out.println( “Object at index “ + index + “ is “ + sobj[index].outCaps() ); for (int index=0; index < 15; index++) System.out.println( “Object at index “ + index + “ is “ + pobj[index].outCaps() ); for (int index=0; index < 15; index++) System.out.println( “Object at index “ + index + “ is “ + wobj[index].outCaps() ); for (int index=0; index < 15; index++) System.out.println( “Object at index “ + index + “ is “ + pobj[index].outCaps() ); } There will be a problem if any of the objects in the array does not provide a method called outCaps, with exactly that signature…

Rather than leaving this up to the individual programmers of the Sentence, Paragraph, Word and BlockText class, it would be nice if the COMPILER could force these classes to implement the methods we need…… We can, if the application program code insists that those classes must IMPLEMENT the Capitalizable interface…………..

Capitalizable [] sobj; Capitalizable [] pobj; Capitalizable [] wobj; Capitalizable [] bobj; sobj = new Capitalizable [100]; pobj = new Capitalizable [100]; wobj = new Capitalizable [100]; bobj = new Capitalizable [100]; sobj[0] = new Sentence(); // this is fine, these objects are Capitalizable pobj[0] = new Paragraph(); wobj[0] = new Word(); bobj[0] = new BlockText(); ….. // other assignments sobj[15] = new Sentence(); pobj[15] = new Paragraph(); wobj[15] = new Word(); bobj[15] = new BlockText();

public class Sentence implements Capitalizable{ // this class will ONLY not compile if it does not contain the methods specified in the Capitalizable interface } public class Paragraph implements Capitalizable{ // this class will ONLY not compile if it does not contain the methods specified in the Capitalizable interface } public class BlockText implements Capitalizable{ // this class will ONLY not compile if it does not contain the methods specified in the Capitalizable interface }

// one array can store varying types of objects // all objects can be output in capitals using the same code – // because it is KNOWN that each object has an outCaps method Capitalizable [] textobj; textobj = new Capitalizable [60]; textobj[0] = new Sentence(); textobj[1] = new Paragraph(); textobj[2] = new Word(); ….. // other assignments textobj[59] = new BlockText(); for (int index=0; index < 60; index++) System.out.println( “Object at index “ + index + “ is “ + textobj[index].outCaps() );

for (int index=0; index < 60; index++) System.out.println( “Object at index “ + index + “ is “ + textobj[index].outCaps() ); //different methods are being executed depending the type of each array element How does JAVA know which method outCaps() is being output at any one time??

The Java language provides ‘polymorphism’, or late binding. A polymorphic language waits until RUN TIME to bind a method call to the actual code that will be run. (this allows Java to KNOW the type of object that is making the call) (An object oriented language provides: encapsulation of data and methods via classes polymorphism inheritance )

You have already seen an interface…. Shape * The Shape interface provides a number of abstract methods, just like any other interface. * Rectangle2D, Line2D, Ellipse2D, RoundRectangle2D, Rectangle2D all realize (implement) the Shape interface. * So a object created from any of these classes is an object of type Shape. * An array declared and instantiated as: Shape [ ] shapelist = new Shape[40]; is capable of storing 40 objects instantiated from any of these classes.

For example: import ……. public class MyShapes extends Applet{ private Shape [ ] shapelist = new Shape[4]; public MyShapes() { //create the draw shapelist[0] = new Rectangle2D.Double(x,y,width,height); shapelist[1] = new Ellipse2D.Double(x,y,width,height); shapelist[2] = new Line2D.Double(x,y,width,height); shapelist[3] = new RoundRectangle2D.Double(x,y,width,height); }

MyShapes class continued…. public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // draw all the shapes for (int index = 0; index < 4; index ++ ) g2.draw(shapelist[index]); }

The MyShapes class does not make use of the fact that all the objects in the array have many method names in common. This applet only uses their common type to allow one array to be used to store all the shapes. HOWEVER, the draw(Shape) method of the Graphics2D class probably does call upon methods of the passed objects. for example: Any object of type Shape has a method called getBoundary(). This method would be quite useful in drawing any shape … and Is probably called within the draw method code..

One method which was in the Array class is the the Arrays class provided: static void sort (Object [] array) static int binarySearch(Object[] array, Object key) This means: Dog[] values = new Dog[55]; Dog adog; …. code which initializes the array Arrays.sort(values); ….. int position = Arrays.binarySearch(values,aDog);

How does sort know how to order an array of Dogs?? How can binarySearch determine if 2 Dog objects are equal?? In order to use these methods with a class type, that class type must define it’s ordering ……… That class type must implement the Comparable interface)

public interface Comparable { public abstract int compareTo(Object obj); } Usage: // This method returns 0 if implicit object (this) is equal to obj // This method returns a negative value if implicit object is less than obj // This method returns a positive value if obj is larger than implicit object

Suppose we wanted to write an applet that would read Dog data from a file, store them in an array, sort them and draw them in order. Our Dog class would have to implement the Comparable interface.. Let’s code that………

another use of interfaces … Suppose we had a class named BankAccounts, where an object of this class represented one bank account. (one method of this class is getBalance, which gives the account balance). Now we want to create a class named BADataSet, which can monitor a group BankAccount objects, keeping track of the total funds and largest account.

BADataSet might look like this: public class BADataSet { private double sum; //total of all account balances private BankAccount maximum; //account w/ highest balance private int count; //number of accounts in set public void add( BankAccount x){ sum = sum + x. getBalance (); if (count == 0 || maximum. getBalance ()< x. getBalance ()) maximum = x; count++; } public BankAccount getMaximum(){ return maximum; }

Suppose we had a class named Coin, where an object of this class represents one coin. (one method of this class is getTotal, which gives the value of the coins). Now we want to create a class named CDataSet, which can keeps track of a number of Coin objects, keeping track of the total cash and largest denomination of coin.

CDataSet might look like this: public class CDataSet { private double sum; //total of all coin objects private Coin maximum; //coin object with highest value private int count; //number of coin objects in set public void add(Coin x){ sum = sum + x. getTotal (); if (count == 0 || maximum. getTotal ()< x. getTotal ()) maximum = x; count++; } public Coin getMaximum(){ return maximum; }

Gee, both of those classes look quite alike. In fact, except for: * getBalance() and getTotal() * parameter/return types it is the same code. How can we write ONE DataSet class which can handle both types of objects??

Suppose both classes could agree on the same method name, say, getMeasure DataSet could call that method: sum = sum + x.getMeasure(); if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; Define an interface: public interface Measurable { public abstract double getMeasure(); }

Remember …. when a class ‘implements’ an interface it must provide a full implementation of the abstract methods in the interface So, if both BankAccount and Coin classes ‘implemented’ the measurable interface, we know they have the method getMeasure

class BankAccount implements Measurable{ double balance; //additional variables and methods public double getMeasure(){ return balance; } } class Coin implements Measurable{ double value; //additional variables and methods public double getMeasure(){ return value; } }

public class DataSet { private double sum; private ********* maximum; private int count; public void add(********* x){ sum = sum + x.getMeasure(); if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; count++; } public ************ getMaximum(){ return maximum; } WE have taken care of the method name, but what data type will work, can’t be BankAccount and can’t be Coin!! }

Remember …. when a class ‘implements’ an interface, objects of that class are object of the interface type (in addition to also being objects of the specific class) So, if both BankAccount and Coin classes ‘implemented’ the Measurable interface, we know they are both objects of class Measurable !

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

// This program tests the DataSet class. public class DataSetTest{ public static void main(String[] args) { DataSet bankData = new DataSet(); bankData.add(new BankAccount(0)); bankData.add(new BankAccount(10000)); bankData.add(new BankAccount(2000)); System.out.println("Average balance = " + bankData.getAverage()); Measurable max = bankData.getMaximum(); System.out.println("Highest balance = " + max.getMeasure());

DataSet coinData = new DataSet(); coinData.add(new Coin(0.25, "quarter")); coinData.add(new Coin(0.1, "dime")); coinData.add(new Coin(0.05, "nickel")); System.out.println("Average coin value = " + coinData.getAverage()); max = coinData.getMaximum(); System.out.println("Highest coin value = " + max.getMeasure()); }

UML Diagram Note that DataSet is decoupled from BankAccount, Coin

Converting Between Types Can convert from class type to realized interface type: BankAccount account = new BankAccount(10000); Measurable x = account; // OK Same interface type variable can hold reference to Coin x = new Coin(0.1, "dime"); // OK Cannot convert between unrelated types x = new Rectangle(5, 10, 20, 30); // ERROR

Casts Add coin objects to DataSet DataSet coinData = new DataSet(); coinData.add(new Coin(0.25, "quarter")); coinData.add(new Coin(0.1, "dime"));... Get largest coin with getMaximum method: Measurable max = coinData.getMaximum(); If the coin class also has a method called ‘getName’ which returns the type of coin as a String…… You know it's a coin, but the compiler doesn't. Apply a cast: Coin maxCoin = (Coin)max; String name = maxCoin.getName(); If you are wrong and max isn't a coin, the compiler throws an exception

The instanceof Operator Use instanceof for safe casts: if (max instanceof Coin) { Coin maxCoin = (Coin)max;... }

object instanceof ClassName Example: if (x instanceof Coin) { Coin c = (Coin)x; } Purpose: To return true if the object is an instance of ClassName (or one of its subclasses), false otherwise

Polymorphism Interface variable x holds reference to object of a class that realizes (implements) the interface Measurable x; x = new BankAccount(10000); x = new Coin(0.1, "dime"); You can call any of the interface methods: double m = x.getMeasure(); Which method is called?

Depends on the actual object. If x refers to a bank account, calls BankAccount.getMeasure If x refers to a coin, calls Coin.getMeasure Java can handle this because it supports polymorphism (greek: many shapes) => one reference can refer to different types of objects In the case of a polymorphic object, the actual method which will be called is determined by the type of the object. How does this work?? The JVM uses the actual type of object to determine method to execute at runtime !! (dynamic or late binding) This is called polymorphism. *Different from overloading. Overloading is resolved by the compiler (at compile time).