C30b: Inner Class, Anonymous Class, and Lambda Expression

Similar documents
C09: Interface, and Abstract Class and Method

C08: Inheritance and Polymorphism

Packages Inner Classes

C12a: The Object Superclass and Selected Methods

CITS2210 Object-Oriented Programming. Topic 10. Java: Nested and Inner Classes

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

*An nested class is a class that is defined inside another class.

Programming overview

Swing: Building GUIs in Java

Swing: Building GUIs in Java

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

C20a: Selected Interfaces in Java API

Declarations and Access Control SCJP tips

C09: Interface and Abstract Class and Method

C18a: Abstract Class and Method

Object Orientation Fourth Story. Bok, Jong Soon

CS 251 Intermediate Programming More on classes

C02: Overview of Software Development and Java

Timing for Interfaces and Abstract Classes

CMSC 132: Object-Oriented Programming II

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

C16b: Exception Handling

Reflection. Based on

C10: Garbage Collection and Constructors

C08: Inheritance and Polymorphism

Topic 6: Inner Classes

Another IS-A Relationship

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College

Programming Languages and Techniques (CIS120)

C11: Garbage Collection and Constructors

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Introduction to Programming Using Java (98-388)

C15: JavaFX: Styling, FXML, and MVC

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

Lecture 5 Tao Wang 1

Lambda Expressions and Java 8 Streams. Jan Trienes, adapted by Th. Dorssers, Pieter van den Hombergh. Contents of this talk.

Interfaces. An interface forms a contract between the object and the outside world.

C24: Web API: Passing Arguments and Parsing Returns

C16a: Model-View-Controller and JavaFX Styling

Interfaces (1/2) An interface forms a contract between the object and the outside world.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Fundamental Concepts and Definitions

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Java Programming. Events and Listeners

C22a: Problem Solving using Recursion

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

CS : Data Structures Michael Schatz. Sept Lecture 5: Iterators

Exercise. Zheng-Liang Lu Java Programming 273 / 324

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Building custom components IAT351

Programming Languages and Techniques (CIS120)

Lecture 04 FUNCTIONS AND ARRAYS

C19: User Datagram and Multicast

C27a: Stack and Queue

CMSC 132: Object-Oriented Programming II

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Chapter 13. Interfaces and Inner Classes

Java: introduction to object-oriented features

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

What about Object-Oriented Languages?

Array. Prepared By - Rifat Shahriyar

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

The Essence of OOP using Java, Nested Top-Level Classes. Preface

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CST141 JavaFX Events and Animation Page 1

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

CSC 1351 The Twelve Hour Exam From Hell

C18: Network Fundamentals and Reliable Sockets

User Interface Programming OOP/Java Primer. Step 4 Some Final Checkups

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

LAMBDA EXPRESSIONS. Summer 2018

CS : Data Structures

Programming Languages and Techniques (CIS120)

Collections and Iterators. Collections

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

2IP15 Programming Methods

Semantic Analysis. Compiler Architecture

Oracle 1Z Java Certified Programmer. Download Full Version :

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Chapter 4 Defining Classes I

And Even More and More C++ Fundamentals of Computer Science

Array Basics: Outline

Nested Loops. A loop can be nested inside another loop.

Java Technologies. Lecture IV. Valdas Rapševičius

Introduction to Java. Handout-1d. cs402 - Spring

C14b: Classless Intradomain Routing

Exercise (Revisited)

CISC 7310X. C05: CPU Scheduling. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/1/2018 CUNY Brooklyn College

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Compaq Interview Questions And Answers

C09: Process Synchronization

Java Programming Tutorial 1

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

C12: JavaFX Scene Graph, Events, and UI Components

Program Correctness and Efficiency. Chapter 2

Transcription:

CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and Lambda Expression Hui Chen Department of Computer & Information Science CUNY Brooklyn College 12/6/2018 CUNY Brooklyn College 1

Outline Discussed Concept of Stage, Scene, and Scene Graph How to build views, representing visual elements of a Graphical User interface Handling events Event To discuss Event dispatcher chain Registering handlers and handling events Writing event handlers efficiently: inner class, anonymous inner class, and Lambda expression 12/6/2018 CUNY Brooklyn College 2

Writing an EventHandler Always follow the pattern: class MyEventHandler implements EventHandler<MouseEvent> { public void handle(mouseevent) { 12/6/2018 CUNY Brooklyn College 3

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

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

Nested Class Java permits one to define a class within another class. Below are 2 of 4 types: Inner class (Non-static nested class) class OuterClass {... class NestedClass {... Static nested class class OuterClass {... static class StaticNestedClass {... 12/6/2018 CUNY Brooklyn College 6

Using Nested Class Logically grouping classes that are only used in one class Can increase encapsulation Can lead to more readable and maintainable code class B { int c; // B only used in A B b = new B(); b.c = 2; class B { int c; B b = new B(); b.c = 2; 12/6/2018 CUNY Brooklyn College 7

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

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

Inner Class: Member of Outer Class: Examples Which one is correct? void method() { B b = new B(); class B { // B only used in A static void method() { B b = new B(); class B { // B only used in A void method() { B b = this.new B(); class B { // B only used in A static void method() { A a = new A(); B b = a.new B(); class B { // B only used in A 12/6/2018 CUNY Brooklyn College 10

Inner Class: Member of Outer Class: Examples Which one is correct? void method() { B b = new B(); class B { // B only used in A static void method() { B b = new B(); class B { // B only used in A void method() { B b = this.new B(); class B { // B only used in A static void method() { A a = new A(); B b = a.new B(); class B { // B only used in A 12/6/2018 CUNY Brooklyn College 11

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

Static Nest Class: Examples Which one is correct or wrong? void method() { B b = new B(); static class B { // B only used in A static void method() { B b = new B(); static class B { // B only used in A void method() { B b = new A.B(); static class B { // B only used in A static void method() { B b = new A.B(); static class B { // B only used in A 12/6/2018 CUNY Brooklyn College 13

Static Nest Class: Examples Which one is correct or wrong? void method() { B b = new B(); static class B { // B only used in A static void method() { B b = new B(); static class B { // B only used in A void method() { B b = new A.B(); static class B { // B only used in A static void method() { B b = new A.B(); static class B { // B only used in A 12/6/2018 CUNY Brooklyn College 14

Nested Class: More Java permits one to define a class within another class. Below are the other 2 of 4 types: Anonymous class class OuterClass {... SomeParentClass c = new SomeParentClass() {... ; Lambda Expression class OuterClass {... SomeFunctionalInterface inf = (p1, p2) -> { do sth with arguments p1 and p2 ; 12/6/2018 CUNY Brooklyn College 15

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. class OuterClass {... { class NestedClass {.... 12/6/2018 CUNY Brooklyn College 16

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

Using Inner or Local Class: Handling Event Observe the example 12/6/2018 CUNY Brooklyn College 18

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

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 class OuterClass {... { ParentClass a = new ParentClass() { 12/6/2018 CUNY Brooklyn College 20

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

Using Anonymous Event Handler: Example Observe the example 12/6/2018 CUNY Brooklyn College 22

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

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/f unction/package-summary.html 12/6/2018 CUNY Brooklyn College 24

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

Lambda Expression A simple way to declare and instantiate a class 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()); ArrayList<Person> personlist = new ArrayList<Person>(); Arrays.sort(personList, (lhs, rhs) -> lhs.getname().compareto(rhs.getname()) 12/6/2018 CUNY Brooklyn College 26

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

EventHandler in Lambda Expression: Example Observe the example 12/6/2018 CUNY Brooklyn College 28

Questions? Nested class Static inner class Inner class Local class Anonymous class Lambda expression 12/6/2018 CUNY Brooklyn College 29