330 likes | 460 Views
What is an Interface??. An interface is a ‘container’ which contains ONLY abstract methods . public interface Capitalizable { public abstract String outCaps() ;
E N D
What is an Interface?? An interface is a ‘container’ 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 Sentence provided a way to output the object data: • in all capitals • with first letter capitalized • in all lower case
public class Sentence implements Capitalizable { String the_data; public Sentence() { // code for this method } public void addWord(String word) { // code for this method } public int howMany() { // code for this method }
//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 Sentence is ALSO an object of type Capitalizable!!!!!!!!!!!
Why might I need to ensure that Sentence offers these methods?? Suppose we wanted to write a program which stored 15 Sentence objects, output them in capital letters, and output the first capital letter in each Sentence;
//Since Sentence implements the Capitalizable interface, Sentence is of type Capitalizable (and has the needed method). If Sentence didn’t implement the Capitalizable interface, this program wouldn’t compile. public static main (String[] args) { Capitalizable [] sobj; sobj = new Capitalizable [16]; // sobj stores Capitalizable references sobj[0] = new Sentence(); // so we can assign a Sentence object! ….. // the other assignments sobj[15] = new Sentence(); for (int index=0; index < 16; index++) System.out.println( “Object at index “ + index + “ is “ + sobj[index].outCaps() ); //the author of this method KNOWS that the outCaps method is there }
Suppose we KNEW that the Sentence class, the Paragraph class, the Word class and the BlockText implemented the Capitalizable interface!! and we wanted to write a program which stored, and manipulated 15 Sentence objects, 15 Paragraph objects, 15 Word object, and 15 BlockText objects
Capitalizable [] textobj; textobj = new Capitalizable [60]; textobj[0] = new Sentence(); textobj[1] = new Paragraph(); textobj[2] = new Word(); textobj[3] = new BlockText(); ….. // other assignments textobj[59] = new BlockText(); // this 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 for (int index=0; index < 16; index++) System.out.println( “Object at index “ + index + “ is “ + textobj[index].outCaps() );
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 implement (realize) 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 JComponent{ 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 paintComponent(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 component only uses their common type to allow one array to be used to the all the shapes. ------------------------------------------------------------- BUT, since the Shape interface is common to the Line2D.Double class, Rectangle2D.Double class and many other classes, any object that is created from one of these classes is known to provide a common set of methods. We can look at the JAVA API documentation to see what these methods are. The Graphics2D method draw(Shape) knows what these methods are, and uses them -- which is why the parameter must be a Shape.
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, and keep track of: 1.) total of all bank account balances 2.) largest balance
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 coin). 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 coin in a collection of coins.
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: * method calls: getBalance() .vs. 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 objects of the interface type as well Eg. A BankAccount object is ALSO a Measurable object • 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 Coinx = new Coin(0.1, "dime"); // OK • Cannot convert between unrelated typesx = new Rectangle(5, 10, 20, 30); // ERROR
Casts • Add coin objects toDataSetDataSet coinData = new DataSet();coinData.add(new Coin(0.25, "quarter"));coinData.add(new Coin(0.1, "dime"));... • Get largest coin withgetMaximummethod: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 interfaceMeasurable 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?
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).