Nested Classes in java


Presentation Description

No description available.


Presentation Transcript

Nested Classes:

Nested Classes in

Concept of Nested Classes:

It is possible to define a class inside other class ; such classes are known as nested classes . Thus, if a class B is defined within class A, then B is known to A, but not outside A. A nested class has access to members including private members of the class in which it is nested. However enclosing class does not have access to the members of the nested class. Concept of Nested Classes

Example of Nested class:

class A //OuterClass { ... class B //NestedClass { ... } } Example of Nested class

Why Use Nested Classes? :

Logical grouping of classes —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. Increased 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. More readable, maintainable code —Nesting small classes within top-level classes places the code closer to where it is used. Why Use Nested Classes?

Types of Nested Classes:

Two types: 1. Static Nested classes: It has static modifier applied. It Cannot access non-static members of enclosing class . It can have any visibility, just like any other member. Types of Nested Classes

static Nested Classes (cont..):

class OuterClass { ... Static class NestedClass { ... } } static Nested Classes (cont..)

Types of Nested classes (cont..):

2. Non-Static Nested classes: It is also known as inner classes. It has access to all variables and methods of its outer/enclosing class. Thus, inner class is in full scope of its enclosing class. Types of Nested classes (cont..)

Inner classes (cont..):

Instances of enclosing or nested classes do not contain each other –they are only associated with each other. An instance of an enclosing class may be associated with multiple instances of inner classes. An instance of an inner class is associated with exactly one instance of the enclosing class. Inner classes (cont..)

Types of Inner classes:

There are two additional types of inner classes . You can declare an inner class within the body of a method . Such a class is known as a local inner class . You can also declare an inner class within the body of a method without naming it . These classes are known as anonymous inner classes . Types of Inner classes

Example of inner/Non-static classes:

//Demonstrate an inner class class Outer { int outer_x=100; void test(){ Inner inner = new Inner(); inner.display(); } //inner class Class Inner { void display() { System.out.println(“display: outer_x = ”+ outer_x); } } } class InnerDemo { public static void main(String args[]) { Outer outr=new outr(); outr.test(); } Example of inner/Non-static classes Output: display: outer_x = 100

Slide 11:

Arigato !! Thank You !!