Java Nested Class
Java Inner Class
• Java inner class or nested class is a class i.e.
declared inside the class or interface.
• We use inner classes to logically group classes
and interfaces in one place so that it can be
more readable and maintainable.
• Additionally, it can access all the members of
outer class including private data members
and methods.
Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
Advantage of java inner classes
• There are basically three advantages of inner
classes in java. They are as follows:
1) Nested classes represent a special type of
relationship that is it can access all the members
(data members and methods) of outer
class including private.
2) Nested classes are used to develop more
readable and maintainable code because it logically
group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
Example of Inner class
class Outer
{
public void display()
{
Inner in=new Inner();
[Link]();
}
class Inner
{
public void show()
{
[Link]("Inside inner");
}
}
}
class Test{
public static void main(String[] args)
{
Outer ot=new Outer();
[Link]();
}
}
• Output:- Inside inner
Types of Nested classes
• There are two types of nested classes non-
static and static nested [Link] non-static
nested classes are also known as inner classes.
1. Non-static nested class(inner class)
a)Member inner class
b)Annomynous inner class
c)Local inner class
2. Static nested class
Non-static Nested class
• Non-static Nested class is most important type of nested
class.
• It is also known as Inner class. It has access to all variables
and methods of Outer class and may refer to them
directly.
• But the reverse is not true, that is, Outer class cannot
directly access members of Inner class.
• One more important thing to notice about an Inner class is
that it can be created only within the scope of Outer class.
• Java compiler generates an error if any code
outside Outer class attempts to instantiate Innerclass.
Java Member inner class
• A non-static class that is created inside a class
but outside a method is called member inner
class.
• Syntax:
class Outer{
//code
class Inner{
//code
}
}
Example of Member inner class
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){[Link]("data is "+data);}
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
[Link] in=[Link] Inner();
[Link]();
}
}
• Output:-
data is 30
Java Anonymous inner class
• A class that have no name is known as
anonymous inner class in java. It should be
used if you have to override method of class
or interface. Java Anonymous inner class can
be created by two ways:
1. Class (may be abstract or concrete).
2. Interface
Java anonymous inner class example using class
abstract class Person{
abstract void eat();
}
class TestAnonymousInner{
public static void main(String args[]){
Person p=new Person(){
void eat(){[Link]("nice fruits");}
};
[Link]();
}
}
Internal working of given code
• Person p=new Person(){
• void eat(){[Link]("nice fruits");}
• };
1. A class is created but its name is decided by the
compiler which extends the Person class and
provides the implementation of the eat()
method.
2. An object of Anonymous class is created that is
referred by p reference variable of Person type.
Internal class generated by the compiler
import [Link];
static class TestAnonymousInner$1 extends Person
{
TestAnonymousInner$1(){}
void eat()
{
[Link]("nice fruits");
}
}
Java anonymous inner class example using interface
interface Eatable{
void eat();
}
class TestAnnonymousInner1{
public static void main(String args[]){
Eatable e=new Eatable(){
public void eat(){[Link]("nice fruits");}
};
[Link]();
}
}
Internal working of given code
• Eatable p=new Eatable(){
• void eat(){[Link]("nice fruits");}
• };
1. A class is created but its name is decided by the
compiler which implements the Eatable
interface and provides the implementation of
the eat() method.
2. An object of Anonymous class is created that is
referred by p reference variable of Eatable type.
Internal class generated by the compiler
import [Link];
static class TestAnonymousInner1$1 implement
s Eatable
{
TestAnonymousInner1$1(){}
void eat(){[Link]("nice fruits");}
}
Java Local inner class
• A class i.e. created inside a method is called
local inner class in java. If you want to invoke
the methods of local inner class, you must
instantiate this class inside the method.
• Rule: Local variable can't be private, public or
protected.
Java local inner class example
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){[Link](data);}
}
Local l=new Local();
[Link]();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
[Link]();
}
}
Example of local inner class with local variable
class localInner2{
private int data=30;//instance variable
void display(){
int value=50;//local variable must be final till jdk 1.7 only
class Local{
void msg(){[Link](value);}
}
Local l=new Local();
[Link]();
}
public static void main(String args[]){
localInner2 obj=new localInner2();
[Link]();
}
}
Java static nested class
• A static class i.e. created inside a class is called
static nested class in java. It cannot access
non-static data members and methods. It can
be accessed by outer class name.
1. It can access static data members of outer
class including private.
2. Static nested class cannot access non-static
(instance) data member or method.
Java static nested class example with instance method
class TestOuter1{
static int data=30;
static class Inner{
void msg(){[Link]("data is "+data);}
}
public static void main(String args[]){
[Link] obj=new [Link]();
[Link]();
}
}
• In this example, you need to create the
instance of static nested class because it has
instance method msg(). But you don't need to
create the object of Outer class because
nested class is static and static properties,
methods or classes can be accessed without
object.
Java static nested class example with static method
• If you have the static member inside static
nested class, you don't need to create
instance of static nested class.
class TestOuter2{
static int data=30;
static class Inner{
static void msg()
{[Link]("data is "+data);}
}
public static void main(String args[]){
[Link]();//
no need to create the instance of static nested class
}
}
Java Nested Interface
• An interface i.e. declared within another
interface or class is known as nested
interface.
• The nested interfaces are used to group
related interfaces so that they can be easy to
maintain.
• The nested interface must be referred by the
outer interface or class. It can't be accessed
directly.
Points to remember for nested interfaces
There are given some points that should be
remembered by the java programmer.
• Nested interface must be public if it is
declared inside the interface but it can have
any access modifier if declared within the
class.
• Nested interfaces are declared static implicitly.
Syntax of nested interface which is declared within the interface
interface interface_name{
...
interface nested_interface_name{
...
}
}
Syntax of nested interface which is declared within the class
class class_name{
...
interface nested_interface_name{
...
}
}
Example of nested interface which is declared within the interface
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface1 implements [Link]{
public void msg(){[Link]("Hello nested interface");}
public static void main(String args[]){
[Link] message=new TestNestedInterface1();//upcasting here
[Link]();
}
}
• As you can see in the above example, we are
accessing the Message interface by its outer
interface Showable because it cannot be
accessed directly. It is just like almirah inside
the room, we cannot access the almirah
directly because we must enter the room first.
In collection framework, sun microsystem has
provided a nested interface Entry. Entry is the
subinterface of Map i.e. accessed by [Link].
Internal code generated by the java compiler for nested interface
Message
public static interface Showable$Message
{
public abstract void msg();
}
Example of nested interface which is declared within the class
class A{
interface Message{
void msg();
}
}
class TestNestedInterface2 implements [Link]{
public void msg(){[Link]("Hello nested interface");}
public static void main(String args[]){
[Link] message=new TestNestedInterface2();//upcasting here
[Link]();
}
}
Can we define a class inside the interface?
• Yes, If we define a class inside the interface,
java compiler creates a static nested class.
Let's see how can we define a class within the
interface:
interface M{
class A{}
}