Download presentation
Presentation is loading. Please wait.
Published byJoanna Holt Modified over 9 years ago
1
Nested References 2 inner reference data types Classes-Interfaces
2
What are the different Nested Ref. Data Types? Nested (inner) classes Nested (inner) Interfaces
3
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.
4
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).
5
What are the different types of Nested classes? nested static class nested dynamic class
6
Nested static classes Can be instanced without instancing the containing class. Class Point3d{ static class Float(){…} static class Double(){…} }
7
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
8
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); }
9
Inner class instead of packages! class java { class awt { class Frame { } class Component { }
10
Making an Anonymous Inner class interface Runnable { public void run(); } Thread t = new Thread( new Runnable() { public void run() { doStuffHere(); } });
11
What can you build an anonymous class from? an interface. an abstract class. non-abstract class.
12
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”;} }
13
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”;} }
14
An anonymous Member class is sometimes called an anonymous class class OuterClass { class namedMember { } Runnable r = new Runnable() { public void run() { // do stuff here }
15
Nomenclature All anonymous classes are inner classes. Anonymous inner class = anonymous class. Convention is to call it an anonymous inner class
16
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
17
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..... } }); }
18
Nomenclature Few people say: local anonymous inner class. Typically call it anonymous inner class
19
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() {...} }
20
PrintFunction class Print { public static void printFunction(Fcn fcn) { for (int x=0; x < 100; x++) System.out.println(fcn.f(x)); }
21
Fcn, The Interface public interface Fcn { double f(double x); } print.printFunction(new Fcn() { double f(double x) { return x*x; } });
22
RunButton new RunButton(“ok”) { public void run() { doesTheOKThing(); }
23
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(); }
24
How do I use Commando? Commando c = new Commando(); Thread t = new Thread(c.getRunnable()); t.start(); //call back on the run method
25
Local inner class A class defined inside of a method Not accessible to outer classes Or the other methods in the same class!
26
Why do I need local inner classes? Factory pattern with an anonymous inner class! For example, a local anonymous inner class!!
27
This is pretty common public Runnable getRunnable() { new Runnable() { public void run() { System.out.println(“wow”); }
28
Named Local Inner class class RunnableThings { Runnable getARunThing() { class Foo extends Job{} class Job implements Runnable { public void run(){} } return new Job(); }
29
Nested Interface public class Roach { interface killable { boolean isAlive(); } class Colony implements killable { boolean isAlive() {return true;} }
30
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.