Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface. Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the.

Similar presentations


Presentation on theme: "Interface. Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the."— Presentation transcript:

1 Interface

2 Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the class. Often the implementation is postponedOften the implementation is postponed until the interface is settled, this can give a more “high-level” construction phase. But also changing and extending the interface without changing the implementation is considered to be a great quality of object- oriented development. The classes which is dependent of the changed class might be subject to a “sound” revision.

3 An Interface Can Be Seen As a Protocol interfaceagreement on behaviorAn interface is an agreement on behavior. classimplements the specified behaviorA class which implements an interface agrees on supporting the specified behavior. interfacedefinition of a roleAn interface can also be seen as a definition of a role, classes which implements the role are able to play that role. Example: The auto-pilot of an airplane can act as a pilot the same is true about some humans. The auto-pilot and the human is quit different types of objects, but they can both play the same role. Many different types of objects can implement the interface, so the interface is not the same as a “class to subclass from”.

4 Interfaces in Java In Java you can explicitly declare an interface In Java you can explicitly declare an interface. Definition (JavaSoft): An interface is a named collection of method definitions (without implementations). An interface can also include constant declarations (no fields). In C++ you can declare interfaces through abstract classes. Java do not have multiple-inheritance Java do not have multiple-inheritance (multiple-inheritance is considered difficult both conceptually and when it comes to implementation of compilers). With interfaces you can achieve much the same as when you use multiple-inheritance. It is possible to inherit from one class and at the same time implement several interfaces.

5 Example All methods in an interface must be public and abstract so it is not necessary to explicitly specify this. Example: If you have a set of objects (of the same type) and you want to sort this objects, then it must be possible for each pair of objects to do a comparison and decide if one object is “larger” then the other. The objects must implement the interface Sortable : public interface Sortable { public int compare(Sortable b); } public interface Sortable { public int compare(Sortable b); }

6 Sorting an Array of Person-objects > Sortable int compare(sortable) Person String name ….. String getName() ….. int compare(sortable) * Register Person[] reg void sort() ….. 1 1 Sort ….. static void bubbleSort(Sortable[])

7 Sorting an Array of Person-objects 2 public class Sort { public static void bubbleSort(Sortable[] array){ // Find index of last used arrayelement int len = array.length; for(;(array[len-1] == null) && (len>0); len--); for(int pass = 1; pass < len; pass++){ for(int i = 0; i<len - 1; i++){ if(array[i].compare(array[i+1]) > 0){ Sortable hold = array[i]; array[i] = array[i+1]; array[i+1] = hold; } public class Sort { public static void bubbleSort(Sortable[] array){ // Find index of last used arrayelement int len = array.length; for(;(array[len-1] == null) && (len>0); len--); for(int pass = 1; pass < len; pass++){ for(int i = 0; i<len - 1; i++){ if(array[i].compare(array[i+1]) > 0){ Sortable hold = array[i]; array[i] = array[i+1]; array[i+1] = hold; }

8 Sorting an Array of Person-objects 3 public class Person implements Sortable{ public int compare(Sortable other){ if (!(other instanceof Person)) return 0; String otherName = ((Person)other).getName(); return name.compareTo(otherName); } …… } public class Person implements Sortable{ public int compare(Sortable other){ if (!(other instanceof Person)) return 0; String otherName = ((Person)other).getName(); return name.compareTo(otherName); } …… } public class Register{ private Person[] reg; public void sort(){ if (noOfPersons==0) return; Sort.bubbleSort(reg); } …… } public class Register{ private Person[] reg; public void sort(){ if (noOfPersons==0) return; Sort.bubbleSort(reg); } …… }

9 The clone-method Object classa byte by byte copyThe Object class has a clone-method which do a byte by byte copy. class Object{ protected native Object clone() throws CloneNotSupportedException{.. } object which is referenced will not be copied reference to the same objectThis method is not always applicable. If you have a reference field, than the object which is referenced will not be copied just the reference to the object. The clone-object and the original object will both have a field which have a reference to the same object. If you want to have a clone- method or use the one from the Object class, than you have to implement the Cloneable interface.Cloning is considered a serious matter. The method is protected, so it can only be used inside a derived class. If you want to have a clone- method or use the one from the Object class, than you have to implement the Cloneable interface.

10 If You Have a cloneable Class Assume: class MyClass implements Cloneable{................}......... MyClass obj1 = new MyClass(...); MyClass obj2 = (MyClass)obj1.clone(); Than the following is typically true : obj1 != obj2 // the two references are to different objects obj1.equals(obj2) // values of the two different objects are compared obj1.clone.getClass() == obj1.getClass() // they are of the same class

11 Making Your Own Clone-method Example No. 1 class Person implements Cloneable { private String name; Person(String newName){ name = newName; } public String getName(){ return name; } public Object clone(){ try{ return super.clone(); // remember Strings are not mutable }catch (CloneNotSupportedException e){ return null; // should not happen, Cloneable is implemented } } } public class TestClone { public static void main(String[] args) { Person p1 = new Person("Olsen"); Person p2 = (Person)p1.clone(); System.out.println("p1.Name: " + p1.getName()); System.out.println("p2.Name: " + p2.getName()); while(true); // let the user see the output } class Person implements Cloneable { private String name; Person(String newName){ name = newName; } public String getName(){ return name; } public Object clone(){ try{ return super.clone(); // remember Strings are not mutable }catch (CloneNotSupportedException e){ return null; // should not happen, Cloneable is implemented } } } public class TestClone { public static void main(String[] args) { Person p1 = new Person("Olsen"); Person p2 = (Person)p1.clone(); System.out.println("p1.Name: " + p1.getName()); System.out.println("p2.Name: " + p2.getName()); while(true); // let the user see the output }

12 Making Your Own Clone-method Example No. 2 - Will Not Work public class TestClone { public static void main(String[] args) { Person p1 = new Person("Jan"); Person p2 = (Person)p1.clone(); StringBuffer temp = p2.getName(); temp = temp.append(" Olsen"); // p1.name is also changed System.out.println("p1.Name: " + p1.getName()); System.out.println("p2.Name: " + p2.getName()); } class Person implements Cloneable { private StringBuffer name; Person(String newName){ name = new StringBuffer(newName); } public StringBuffer getName(){ return name; } public Object clone(){ try{ return super.clone(); // NOTE!!! the following is not good enough // StringBuffer is mutable and a reference to the same // object can give you problems }catch (CloneNotSupportedException e){ return null; // should not happen, Cloneable is implemented } } } public class TestClone { public static void main(String[] args) { Person p1 = new Person("Jan"); Person p2 = (Person)p1.clone(); StringBuffer temp = p2.getName(); temp = temp.append(" Olsen"); // p1.name is also changed System.out.println("p1.Name: " + p1.getName()); System.out.println("p2.Name: " + p2.getName()); } class Person implements Cloneable { private StringBuffer name; Person(String newName){ name = new StringBuffer(newName); } public StringBuffer getName(){ return name; } public Object clone(){ try{ return super.clone(); // NOTE!!! the following is not good enough // StringBuffer is mutable and a reference to the same // object can give you problems }catch (CloneNotSupportedException e){ return null; // should not happen, Cloneable is implemented } } }

13 Making Your Own Clone-method Example No. 3 - This Will Work Ok public class TestClone { public static void main(String[] args) { Person p1 = new Person("Jan"); Person p2 = (Person)p1.clone(); StringBuffer temp = p2.getName(); temp = temp.append(" Olsen"); // p2.name will not be changed System.out.println("p1.Name: " + p1.getName()); System.out.println("p2.Name: " + p2.getName()); } class Person implements Cloneable { private StringBuffer name; Person(String newName){ name = new StringBuffer(newName); } public StringBuffer getName(){ return name; } public Object clone(){ try{ // this will work Person temp = (Person)super.clone(); temp.name = new StringBuffer(name.toString()); return temp; }catch (CloneNotSupportedException e){ return null; // should not happen, Cloneable is implemented } } } public class TestClone { public static void main(String[] args) { Person p1 = new Person("Jan"); Person p2 = (Person)p1.clone(); StringBuffer temp = p2.getName(); temp = temp.append(" Olsen"); // p2.name will not be changed System.out.println("p1.Name: " + p1.getName()); System.out.println("p2.Name: " + p2.getName()); } class Person implements Cloneable { private StringBuffer name; Person(String newName){ name = new StringBuffer(newName); } public StringBuffer getName(){ return name; } public Object clone(){ try{ // this will work Person temp = (Person)super.clone(); temp.name = new StringBuffer(name.toString()); return temp; }catch (CloneNotSupportedException e){ return null; // should not happen, Cloneable is implemented } } }


Download ppt "Interface. Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the."

Similar presentations


Ads by Google