Nested Classes Yoshi Modified from:

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Access to Names Namespaces, Scopes, Access privileges.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Nested Classes  Learning Outcome  List and distinguish between the different categories of nested classes  List and explain four applications/benefits.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Nested Classes OOP tirgul No Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;
Inner Classes in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Nested References 2 inner reference data types Classes-Interfaces.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Nested Classes CompSci 230 S Software Construction.
CS 61B Data Structures and Programming Methodology July 8, 2008 David Sun.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
NESTED CLASSES REFLECTION PROXIES.
Classes (Part 1) Lecture 3
CompSci 230 S Programming Techniques
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
Interface.
Overloading and Overriding
Classes & Objects: Examples
Inner Classes 29-Nov-18.
Inner Classes.
Namespaces, Scopes, Access privileges
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Chapter 9 Objects and Classes
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Chapter 11 Inheritance and Encapsulation and Polymorphism
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Presentation transcript:

Nested Classes Yoshi Modified from:

Categories Nested – With static modifier Static nested classes – Without static modifier Inner Classes – Anonymous (inner) classes – Local classes

Example class OuterClass {... static class StaticNestedClass {... } class InnerClass {... }

First, we need to know… Nested classes are a kind of coding style – We can do the same things without using nested classes – However, it has some benefits

Why Use Nested Classes? It is a way of logically grouping classes that are only used in one place. – If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Why Use Nested Classes? (2) It increases encapsulation. – Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

Why Use Nested Classes? (3) Nested classes can lead to more readable and maintainable code. – Nesting small classes within top-level classes places the code closer to where it is used.

Static Nested Classes Simplest one A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. Example – OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

This Figure illustrates … An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Example class Invoice { private int totalPrice=0; private class InvoiceItem { InvoiceItem(int price) { totalPrice+=price; } //What does this example imply?

Shadowed Variables class Outer { int var = 3; class Inner { int var = 5; //there is also a variable called "var" Inner() { System.out.println(var); System.out.println(Outer.this.var); } public static void main(String[] args) { Outer outerObj = new Outer(); Outer.Inner innerObj = outerObj.new Inner(); }

Anonymous Inner Classes public class UseInnerClass { public static void main(String[] args) { Object obj = new Object() { public String toString() { return " 匿名類別物件 "; } }; System.out.println(obj.toString()); } }

Anonymous Inner Classes (2) public void someMethod() { final int x = 10; /*if without final keyword, compiler says: local variable x is accessed from within inner class; needs to be declared final */ Object obj = new Object() { {.. } //instance constructor public String toString() { return "" + x; } }; System.out.println(obj.toString()); }

Local Classes Similar to anonymous inner classes but have a given name Relatively few chances to use

Local Classes (2) public class UseInnerClass { public static void main(String[] args) { class LocalClass extends Object { public String toString() { return " 匿名類別物件 "; } } LocalClass obj = new LocalClass(); System.out.println(obj.toString()); } }

Modifier Top-level classes – Public – Default Nested classes – Treated as a normal member of a class Public Protected Default Private

Reference Java Tutorial – OO/nested.html OO/nested.html Thinking in Java – ming_books/thinking_in_java/TIJ310_016.htm ming_books/thinking_in_java/TIJ310_016.htm Caterpillar’s Tutorial (Simpler) – V1/InnerClass.htm V1/InnerClass.htm

Exercise javaOO/QandE/nested-questions.html