Nested References 2 inner reference data types Classes-Interfaces
What are the different Nested Ref. Data Types? Nested (inner) classes Nested (inner) Interfaces
What is a nested reference? A reference contained by another reference A reference contained by a method A reference with no name ->anonymous anonymous class.
Why do I need a nested reference? To create a temporary reference To isolate the reference name space To create a new reference organization To make use of new object-oriented designs To create a local reference Simplify interfaces (façade design pattern).
What are the different types of Nested classes? nested static class nested dynamic class
Nested static classes Can be instanced without instancing the containing class. Class Point3d{ static class Float(){…} static class Double(){…} }
What kinds of access can I have for a static class? public – public ref organization default – package ref organization private – yes, you can have a private inner class! protected – accessible to subclasses of the outer class
A static inner class public abstract class Point3d { public static class Float extends Point3d { public Float(float x, float y, float z) { … } public static class Double extends Point3d { public Double (double x, double y, double z) {...} } Point3d pf = new Point3d.Float(10,20,30); }
Inner class instead of packages! class java { class awt { class Frame { } class Component { }
Making an Anonymous Inner class interface Runnable { public void run(); } Thread t = new Thread( new Runnable() { public void run() { doStuffHere(); } });
What can you build an anonymous class from? an interface. an abstract class. non-abstract class.
Example of an anonymous inner class from a non-abstract class. class Customer { private String name = “j doe”; public String toString() { return name;} } class Payroll { Customer c = new Customer(){public String toString() {return “hello world”;} }
Example of an named inner class from a non-abstract class. class Customer { private String name = “j doe”; public String toString() { return name;} } class Payroll { class Customer2 extends Customer{ public String toString() {return “hello world”;} }
An anonymous Member class is sometimes called an anonymous class class OuterClass { class namedMember { } Runnable r = new Runnable() { public void run() { // do stuff here }
Nomenclature All anonymous classes are inner classes. Anonymous inner class = anonymous class. Convention is to call it an anonymous inner class
Why not use a named class? class Job implements Runnable { public void run() { do Stuff Here... } Thread t = new Thread(new Job()); Ans: you only needed one new job. Like singleton
Make a 1000 classes.... Local anonymous inner class public static void main(String args[]){ for (int i=0; i < 1000; i++) new Foo(new Runnable() { public void run() { This a unique class..... } }); }
Nomenclature Few people say: local anonymous inner class. Typically call it anonymous inner class
Command Pattern A design pattern Have commands reside in the instance, so the instance knows how to run itself. new Job(2) { public void doCommand() {...} }
PrintFunction class Print { public static void printFunction(Fcn fcn) { for (int x=0; x < 100; x++) System.out.println(fcn.f(x)); }
Fcn, The Interface public interface Fcn { double f(double x); } print.printFunction(new Fcn() { double f(double x) { return x*x; } });
RunButton new RunButton(“ok”) { public void run() { doesTheOKThing(); }
Using the Interface...for commands class Commando { public Runnable getRunnable() { // factory pattern. return new Runnable() { // anonymous local class public void run() { System.out.println(“wow”); } // Commando c = new Commando(); } // Runnable r = c.getRunnable(); }
How do I use Commando? Commando c = new Commando(); Thread t = new Thread(c.getRunnable()); t.start(); //call back on the run method
Local inner class A class defined inside of a method Not accessible to outer classes Or the other methods in the same class!
Why do I need local inner classes? Factory pattern with an anonymous inner class! For example, a local anonymous inner class!!
This is pretty common public Runnable getRunnable() { new Runnable() { public void run() { System.out.println(“wow”); }
Named Local Inner class class RunnableThings { Runnable getARunThing() { class Foo extends Job{} class Job implements Runnable { public void run(){} } return new Job(); }
Nested Interface public class Roach { interface killable { boolean isAlive(); } class Colony implements killable { boolean isAlive() {return true;} }
Summary two kinds of nest ref data types: interface local inner interface not permitted! member inner interface class local inner class (named or anonymous) member inner class (named or anonymous)