1 / 57

Interfaces & Inner Classes

Interfaces & Inner Classes. Structures to Assist In Organizing Complex Class Relationships. Acceptable Implicit Casts for Class References. A method developed to work with this class. it should work with all of its descendants. Reality Rarely Fits Into a Simple Tree. A method developed

vaughnb
Télécharger la présentation

Interfaces & Inner Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces & Inner Classes Structures to Assist In Organizing Complex Class Relationships

  2. Acceptable Implicit Casts for Class References A method developed to work with this class ... it should work with all of its descendants.

  3. Reality Rarely Fits Into a Simple Tree A method developed to work with this class ... State License should work with all of its descendants.

  4. Reality Rarely Fits Into a Simple Tree A method developed to work with this class ... Prescription License should work with all of its descendants.

  5. The Problem of Multiple Inheritance meth() meth() meth() If I send an object, of this class, a meth() message which implementation is used?

  6. The Interface is Java's Solution meth() meth() meth() If I send an object, of this class, a meth() message which implementation is used?

  7. Interfaces • An Interface is a Class with No Implementation • An Interface Contains Only: • Method Names • Argument Lists • Return Types • Fields (static and final only) • An Interface is a Kind of Contract

  8. ReviewAbstract Methods and Classes

  9. A Hierarchy of Pets class Pet { String name; void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Say What?"); } } void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  10. A Hierarchy of Pets class Pet { String name; void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Say What?"); } } void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  11. A Hierarchy of Pets class Pet { String name; void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Say What?"); } } void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  12. The Sounds of a Pet Store Woof Woof Tweet Tweet Woof Woof Tweet Tweet Woof Woof Tweet Tweet Woof Woof Tweet Tweet Woof Woof Tweet Tweet class Pet { String name; void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Say What?"); } } void setName(String s) { name = s; } String getName() { return name; } } public class PetStore { public static void main(String[] args) { Pet[] p = new Pet[10]; for(int i = 0; i<10; i+=2) { p[i] = new Dog(); p[i+1] = new Bird(); } for (int i = 0; i<10; i++) { p[i].speak(2); } } }

  13. The Sounds of a Pet Store class Pet { String name; void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Say What?"); } } void setName(String s) { name = s; } String getName() { return name; } } What is this speak() for? public class PetStore { public static void main(String[] args) { Pet[] p = new Pet[10]; for(int i = 0; i<10; i+=2) { p[i] = new Dog(); p[i+1] = new Bird(); } for (int i = 0; i<10; i++) { p[i].speak(2); } } }

  14. Declare the Class Abstract class Pet { String name; void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Say What?"); } } void setName(String s) { name = s; } String getName() { return name; } } abstract class Pet { String name; abstract void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } abstract class Pet { String name; abstract void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } Compiles and Runs. class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  15. Lets Make Pet an Interface abstract class Pet { String name; abstract void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } abstract class Pet { String name; abstract void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } class Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  16. Lets Make Pet an Interface interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  17. Lets Make Pet an Interface interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } interface Pet { void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } interface Pet { void speak(int i); void setName(String s); String getName(); } interface Pet { void speak(int i); void setName(String s); String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } }

  18. Lets Make Pet an Interface Compiles Ok interface Pet { void speak(int i); void setName(String s); String getName(); } Same problems in Dog class Bird implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } Same for setName() and speak( ...) getName() in Bird cannot implement getName() in Pet; attempting to assign weaker access privileges; was public class Bird implements Pet { ^

  19. Lets Make Pet an Interface Less Capable More Capable interface Pet { void speak(int i); void setName(String s); String getName(); } Methods defined in an interface are public. (The keyword public is not needed.) class Bird implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } Result: Access cannot get more restrictive as you move down the hierarchy. Implemented methods from interfaces must be public.

  20. Lets Make Pet an Interface Less Capable More Capable interface Pet { void speak(int i); void setName(String s); String getName(); } class Bird implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Bird implements Pet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; public void setName(String s) { name = s; } public String getName() { return name; } } class Dog implements Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; void setName(String s) { name = s; } String getName() { return name; } } class Dog implements Pet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; public void setName(String s) { name = s; } public String getName() { return name; } }

  21. Lets Make Pet an Interface Compiles and Runs Woof Woof Tweet Tweet Woof Woof Tweet Tweet Woof Woof Tweet Tweet Woof Woof Tweet Tweet Woof Woof Tweet Tweet interface Pet { void speak(int i); void setName(String s); String getName(); } class Bird implements Pet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; public void setName(String s) { name = s; } public String getName() { return name; } } class Dog implements Pet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; public void setName(String s) { name = s; } public String getName() { return name; } } public class PetStore { public static void main(String[] args) { Pet[] p = new Pet[10]; for(int i = 0; i<10; i+=2) { p[i] = new Dog(); p[i+1] = new Bird(); } for (int i = 0; i<10; i++) { p[i].speak(2); } } }

  22. Lets Make Pet an Interface interface Pet { void speak(int i); void setName(String s); String getName(); } class Bird implements Pet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } String name; public void setName(String s) { name = s; } public String getName() { return name; } } class Dog implements Pet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } String name; public void setName(String s) { name = s; } public String getName() { return name; } }

  23. Lets Make Pet an Interface interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  24. Lets Make Pet an Interface interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  25. Lets Make Pet an Interface interface Pet { void speak(int i); void setName(String s); String getName(); } interface Pet { void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } interface Pet { void speak(int i); void setName(String s); String getName() { return name; } } interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } public String getName() { return name; } } abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } } abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } public String getName() { return name; } } abstract class APet implements Pet{ } abstract class APet implements Pet{ String name; } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird extends APet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } } class Dog extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } } class Dog extends APet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } } class Dog extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }

  26. Collection Map Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap

  27. Inner Classes Interfaces It's About Classes With Inner Class

  28. A Simple Inner Class class Outer { private int i = 10; class Inner { int k = 5; void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.meth(); } } k = 5 i = 10 k = 6 i = 11 k = 7 i = 12

  29. Hiding The Inner Class class Outer { private int i = 10; class Inner { int k = 5; void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; class Inner { int k = 5; void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; class Inner implements In { int k = 5; void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } class Inner implements In { int k = 5; void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } class Inner implements In { int k = 5; void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } class Inner implements In { int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } private class Inner implements In { int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } private class Inner implements In { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } private class Inner implements In { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } k = 5 i = 10 k = 6 i = 11 k = 7 i = 12

  30. The Anonymous Inner Class interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new Inner(); } private class Inner implements In { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new In() ; } private class Inner implements In { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new In() ; } private class Inner implements In { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } }

  31. The Anonymous Inner Class interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new In() ; } private class Inner implements In { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new In() { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } }; } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } interface In { void meth(); } class Outer { private int i = 10; public In getInner() { return new In() { private int k = 5; public void meth() { for(int j = 0; j < 3; j++) { System.out.println("k = " + k++); System.out.println("i = " + i++); } } }; } } public class DoOuter { public static void main(String[] args) { Outer o = new Outer(); In i = o.getInner(); i.meth(); } } k = 5 i = 10 k = 6 i = 11 k = 7 i = 12

  32. A Change of Topic The Simple Inner Class A Closure Example

  33. A Closure • A callable object that retains Information, and access to information, from the scope where it was created.

  34. Your Classes r Library Classes The Closure An Interface specifies the inner class method interface • Using Class • Make a Outer Class • Get an inner class and reference made for you • Apply the Inner class methods The Outer Class with A Method to return an object reference to an inner class Inner Class provides controlled access to outer class information

  35. class List { int[] list = new int[100]; int addPoint = 0; int readPoint = 0; void add(int i) { list[addPoint++] = i; } int read() { return list[readPoint++]; } } public class DoList { public static void main(String[] args) { List l = new List(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } Filling and Reading A List 0 10 20 30 40

  36. class List { int[] list = new int[100]; int addPoint = 0; int readPoint = 0; void add(int i) { list[addPoint++] = i; } int read() { return list[readPoint++]; } } public class DoList { public static void main(String[] args) { List l = new List(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; int readPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; int readPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; int readPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(l.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } } } Using An Inner Class 0 10 20 30 40

  37. class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } Using An Inner Class 0 10 20 30 40 0 10 20 30 40

  38. class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader implements ListReader { int readPoint = 0; int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; void add(int i) { list[addPoint++] = i; } class Reader implements ListReader { int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; ListReader getReader() { return new Reader(); } void add(int i) { list[addPoint++] = i; } class Reader implements ListReader { int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; ListReader getReader() { return new Reader(); } void add(int i) { list[addPoint++] = i; } class Reader implements ListReader { int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; ListReader getReader() { return new Reader(); } void add(int i) { list[addPoint++] = i; } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } Hiding the Implementation List.Reader has private access in List List.Reader r1 = l.new Reader() ^

  39. interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; ListReader getReader() { return new Reader(); } void add(int i) { list[addPoint++] = i; } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); List.Reader r1 = l.new Reader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; ListReader getReader() { return new Reader(); } void add(int i) { list[addPoint++] = i; } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); ListReader r1 = l.getReader(); List.Reader r2 = l.new Reader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { int[] list = new int[100]; int addPoint = 0; ListReader getReader() { return new Reader(); } void add(int i) { list[addPoint++] = i; } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); ListReader r1 = l.getReader(); ListReader r2 = l.getReader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { private int[] list = new int[100]; private int addPoint = 0; public ListReader getReader() { return new Reader(); } public void add(int i) { list[addPoint++] = i; } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); ListReader r1 = l.getReader(); ListReader r2 = l.getReader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } interface ListReader ( int read(); } class List { private int[] list = new int[100]; private int addPoint = 0; public ListReader getReader() { return new Reader(); } public void add(int i) { list[addPoint++] = i; } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } } } public class DoList { public static void main(String[] args) { List l = new List(); ListReader r1 = l.getReader(); ListReader r2 = l.getReader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } } } Hiding the Implementation 0 10 20 30 40 0 10 20 30 40

  40. Your Classes r Library Classes The Closure An Interface specifies the inner class method interface "ListReader" specifies the inner class method interface • Using Class • Make a Outer Class • Get an inner class and reference made for you • Apply the Inner class methods • "DoList" • Make a Outer Class • Get an inner class and reference made for you • Apply the Inner class methods The Outer Class with A Method to return an object reference to an inner class "List" with A Method to return an object "getReader()" Inner Class provides controlled access to outer class information "Reader" provides controlled access to outer class information

  41. Collection Map Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap

  42. A Change of Topic A Closure Example A Callback Example

  43. Your Classes r Library Classes The Callback An Interface specifies the action method interface An Outer Class A Class with Action Method The Button Class with A Method to receive and keep an object reference • Class Performing Setup • Make a Button • Make a class with an action method • Give the button a reference to the action method class • Activate the Button

  44. import java.io.*; class ThinkButton { void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } The Imaginary Button

  45. import java.io.*; class ThinkButton { void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } The Imaginary Button Imagine a Button & Press Return OK I am doing whatever Imagine a Button & Press Return OK I am doing whatever Imagine a Button & Press Return OK I am doing whatever Imagine a Button & Press Return OK I am doing whatever

  46. import java.io.*; class ThinkButton { void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class ThinkButton { void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; System.out.println("OK I am doing whatever"); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } A Callback

  47. import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } A Callback

  48. import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } A Callback Imagine a Button & Press Return OK I am doing whatever Imagine a Button & Press Return OK I am doing whatever Imagine a Button & Press Return OK I am doing whatever Imagine a Button & Press Return OK I am doing whatever

  49. Your Classes r Library Classes The Callback An Interface specifies the action method interface A Class with Action Method The Button Class with A Method to receive and keep an object reference • Class Performing Setup • Make a Button • Make a class with an action method • Give the button a reference to the action method class • Activate the Button

  50. import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new What() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new ActionListener() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class What implements ActionListener{ public void actionPerformed() { System.out.println("OK I am doing whatever"); } } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new ActionListener() ); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new ActionListener() { public void actionPerformed() { System.out.println("OK I am doing whatever"); } }); tb.doButtonThing(); } } import java.io.*; interface ActionListener { void actionPerformed(); } class ThinkButton { ActionListener whoToCall; void tellWho(ActionListener al) { whoToCall = al; } void doButtonThing() throws IOException { for(int c = 1; c < 5; c++) { System.out.println("Imagine a Button & Press Return"); while( System.in.read() != '\r') {}; whoToCall.actionPerformed(); } } } public class Callback { public static void main(String[] args) throws IOException { ThinkButton tb = new ThinkButton(); tb.tellWho( new ActionListener() { public void actionPerformed() { System.out.println("OK I am doing whatever"); } }); tb.doButtonThing(); } } Anonymous Class

More Related