cisc 3115 ty3 c30b: inner class, anonymous class, and ...class •can increase encapsulation •can...
TRANSCRIPT
![Page 1: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/1.jpg)
CISC 3115 TY3
C30b: Inner Class, Anonymous Class, and Lambda Expression
Hui Chen
Department of Computer & Information Science
CUNY Brooklyn College
12/6/2018 1CUNY | Brooklyn College
![Page 2: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/2.jpg)
Outline
• Discussed
• Concept of Stage, Scene, and Scene Graph
• How to build views, representing visual elements of a Graphical User interface
• Handling events
• Event
• Event dispatcher chain
• Registering handlers and handling events
• To discuss
• Writing event handlers efficiently: inner class, anonymous inner class, and Lambda expression
12/6/2018 CUNY | Brooklyn College 2
![Page 3: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/3.jpg)
Writing an EventHandler
• Always follow the pattern:
class MyEventHandler implements EventHandler<MouseEvent> {
public void handle(MouseEvent) {
…
}
}
12/6/2018 CUNY | Brooklyn College 3
![Page 4: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/4.jpg)
Writing Event Handler: Inconvenience • Using the pattern, we have to create many small classes, each has only a few lines
of code
• Soon, we will have many classes and files
• MyEventHandler.java
• HerEventHandler.java
• HisEventHandler.java
• TheirEventHandler.java
• OurEventHandler.java
• YourEventHandler.java
• DoNotKnowHowToNameEventHandler.java
• TiredOfNamingItEventHandler.java
• ThereIsEvenMoreEventHandler.java
• ….
12/6/2018 CUNY | Brooklyn College 4
![Page 5: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/5.jpg)
Using Nested Class
• Nested class
• In Java, a class can be defined anywhere within a class
• Java has 4 types of nested classes
12/6/2018 CUNY | Brooklyn College 5
![Page 6: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/6.jpg)
Nested Class
• Java permits one to define a class within another class. Below are 2 of 4 types:
• Inner class (Non-static nested class)
• Static nested class
12/6/2018 CUNY | Brooklyn College 6
class OuterClass {...class NestedClass { ... }
}
class OuterClass {...static class StaticNestedClass { ... }
}
![Page 7: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/7.jpg)
Using Nested Class
• Logically grouping classes that are only used in one class
• Can increase encapsulation
• Can lead to more readable and maintainable code
12/6/2018 CUNY | Brooklyn College 7
class B {int c;
}class A { // B only used in A
B b = new B();b.c = 2;
}
class A {class B {
int c;}B b = new B();b.c = 2;
}
![Page 8: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/8.jpg)
Inner class
• An inner class is a member of the outer class
• have access to other members of the enclosing class, even if they are declared private.
• An inner class can be declared private, public, protected, or package private.
• However, the outer classes can only be declared public or package private
12/6/2018 CUNY | Brooklyn College 8
![Page 9: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/9.jpg)
Inner Class: Member of Outer Class• An instance of the inner class is a part of an
instance of the outer class
• How about create an object of the inner class
12/6/2018 CUNY | Brooklyn College 9
![Page 10: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/10.jpg)
Inner Class: Member of Outer Class: Examples• Which one is correct?
12/6/2018 CUNY | Brooklyn College 10
class A {void method() {
B b = new B();}class B { // B only used in A}
}
class A {void method() {
B b = this.new B();}class B { // B only used in A}
}
class A {static void method() {
B b = new B();}class B { // B only used in A}
}
class A {static void method() {
A a = new A();B b = a.new B();
}class B { // B only used in A }
}
![Page 11: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/11.jpg)
Inner Class: Member of Outer Class: Examples• Which one is correct?
12/6/2018 CUNY | Brooklyn College 11
class A {void method() {
B b = new B();}class B { // B only used in A}
}
class A {void method() {
B b = this.new B();}class B { // B only used in A}
}
class A {static void method() {
B b = new B();}class B { // B only used in A}
}
class A {static void method() {
A a = new A();B b = a.new B();
}class B { // B only used in A }
}
![Page 12: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/12.jpg)
Static Nested Class
• A static nested class is associated with its outer class
• It belongs to the outer class, not to an object of the outer class.
• Behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
12/6/2018 CUNY | Brooklyn College 12
![Page 13: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/13.jpg)
Static Nest Class: Examples
• Which one is correct or wrong?
12/6/2018 CUNY | Brooklyn College 13
class A {void method() {
B b = new B();}static class B { // B only used in A }
}
class A {void method() {
B b = new A.B();}static class B { // B only used in A }
}
class A {static void method() {
B b = new B();}static class B { // B only used in A }
}
class A {static void method() {
B b = new A.B();}static class B { // B only used in A }
}
![Page 14: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/14.jpg)
Static Nest Class: Examples
• Which one is correct or wrong?
12/6/2018 CUNY | Brooklyn College 14
class A {void method() {
B b = new B();}static class B { // B only used in A }
}
class A {void method() {
B b = new A.B();}static class B { // B only used in A }
}
class A {static void method() {
B b = new B();}static class B { // B only used in A }
}
class A {static void method() {
B b = new A.B();}static class B { // B only used in A }
}
![Page 15: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/15.jpg)
Nested Class: More
• Java permits one to define a class within another class. Below are the other 2 of 4 types:
• Anonymous class
• Lambda Expression
12/6/2018 CUNY | Brooklyn College 15
class OuterClass {...SomeParentClass c = new SomeParentClass() { ... };
}
class OuterClass {...SomeFunctionalInterface inf = (p1, p2) -> { …do sth with arguments p1 and p2 };
}
![Page 16: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/16.jpg)
Local Class
• Classes defined within a block
• A block: what between a pair of balanced braces i.e., { }
• A block can be used anywhere a single statement is allowed.
12/6/2018 CUNY | Brooklyn College 16
class OuterClass {...
{ … class NestedClass { ... }…
}….
}
![Page 17: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/17.jpg)
Local Class: Characteristics
• Local classes are similar to inner classes
• A local class has access to the members of its enclosing class.
• In addition, a local class has access to final or effectively final local variables
• Final variables: e.g., final int a;
• Effectively final, e.g., int a = 1; but variable “a” never changes after initialization
• It can access the method’s parameters
• However,
• cannot declare static initializers or member interfaces in a local class.
• can only have static members only when they are constants (final static …)
12/6/2018 CUNY | Brooklyn College 17
![Page 18: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/18.jpg)
Using Inner or Local Class: Handling Event• Observe the example
12/6/2018 CUNY | Brooklyn College 18
![Page 19: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/19.jpg)
Using Anonymous Class: Motivation• If we only create one instance of an inner
class, we still have to write
class MyInnerClass implements SomeInterface {
…
}
• Using anonymous class to reduce these boiler plate code
12/6/2018 CUNY | Brooklyn College 19
![Page 20: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/20.jpg)
Anonymous Class
• Essentially, a local class without a name
• Created by declaring and instantiating a class at the same time
• Use it when need a local class only once
12/6/2018 CUNY | Brooklyn College 20
class OuterClass {...
{ … ParentClass a = new ParentClass() { …}
}…
}
![Page 21: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/21.jpg)
Anonymous Classes are Local Classes• It has access to the members of its enclosing class.
• In addition, it has access to final or effectively final local variables
• Final variables: e.g., final int a;
• Effectively final, e.g., int a = 1; but variable “a” never changes after initialization
• It can access the method’s parameters
• However,
• cannot declare static initializers or member interfaces in a local class.
• can only have static members only when they are constants (final static …)
12/6/2018 CUNY | Brooklyn College 21
![Page 22: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/22.jpg)
Using Anonymous Event Handler: Example• Observe the example
12/6/2018 CUNY | Brooklyn College 22
![Page 23: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/23.jpg)
Functional Interface
• Any interface that contains only one abstract method
• Since Java 8, a functional interface may contain one or more default methods or static methods (these are not abstract methods)
12/6/2018 CUNY | Brooklyn College 23
![Page 24: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/24.jpg)
Use Functional Interface
• In your own program, sometimes a functional interface is a better design choice
• More often, you use functional interfaces because some Java API methods require them
• Examples:
• https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
12/6/2018 CUNY | Brooklyn College 24
![Page 25: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/25.jpg)
Functional Interface and Anonymous Class• You can declare and instantiate a local class, or more often an
anonymous class
• Example
ArrayList<Person> personList = new ArrayList<Person>();
Arrays.sort(personList, new Comparator<Person> {
@Override
public int compare(Person lhs, Person rhs) {
// buggy (what if rhs is null?)
return lhs.getName().compareTo(rhs.getName());
}
}
12/6/2018 CUNY | Brooklyn College 25
![Page 26: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/26.jpg)
Lambda Expression
• A simple way to declare and instantiate a class
12/6/2018 CUNY | Brooklyn College 26
ArrayList<Person> personList = new ArrayList<Person>();Arrays.sort(personList, new Comparator<Person> {
@Overridepublic int compare(Person lhs, Person rhs) {
// buggy (what if rhs is null?)return lhs.getName().compareTo(rhs.getName());
}}
ArrayList<Person> personList = new ArrayList<Person>();Arrays.sort(personList, (lhs, rhs) -> lhs.getName().compareTo(rhs.getName())}
![Page 27: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/27.jpg)
EventHandler is a Functional Interface• We can use Lambda expression to reduce
boiler plate code further when writing event handlers
12/6/2018 CUNY | Brooklyn College 27
![Page 28: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/28.jpg)
EventHandler in Lambda Expression: Example• Observe the example
12/6/2018 CUNY | Brooklyn College 28
![Page 29: CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and ...class •Can increase encapsulation •Can lead to more readable and maintainable code 12/6/2018 CUNY | Brooklyn College 7](https://reader034.vdocuments.mx/reader034/viewer/2022050208/5f5ade52e079b0245d7eece9/html5/thumbnails/29.jpg)
Questions?
• Nested class
• Static inner class
• Inner class
• Local class
• Anonymous class
• Lambda expression
12/6/2018 CUNY | Brooklyn College 29