Inner Classes


Sometimes we can declare a class inside another class, such type of classes are called Inner Classes.
Inner Classes concept was introduced in java 1.1 version to fix GUI bugs as the part of Event handling.
But because of powerful features and benefits of inner classes slowly programmers started using even in regular coding also.
Without existing one type of object if there is no chance of existing another type object, then we should go for Inner class concept.
Examples:
1) Without existing car object if there is no chance of existing wheel object then we should go for Inner Classes.
We have to declare wheel class with in the car class
class car
{
class wheel
{
}
}

2) Without existing Bank object there is no chance of existing account object, Hence we have to define account class inside Bank class
class Bank
{
class Account
{
}
}
3) A map is a collection of key value pass and each key-value pass is called Entry. Without existing map object there is no chance of existing entry object. Hence interface entry is defined inside map interface.
interface Map
{
interface Entry
{
}
}


Notes:
The relationship between outer and inner classes is not parent to child relationship. It is has-A relationship.
Based on the purpose and position of declaration all inner classes are divided into 4 types.
1) Normal or regular Inner Classes
2) Methods local Inner Classes
3) Annoymous Inner Classes(without class name)
4) static nested classes

From static nested class we can access only static members of outer class directly. But in normal Inner classes we can access both static and non-static members of outer class directly.

Normal or regular Inner class:
If we declare any named class directly inside a class without static modifier, such type of class is called "Normal or regular Inner class" .
Example:


RUn Time Error: NosuchmethodError=main



javac outer.java
java outer
Output: outer class main method

java Outer/Inner
Output: NoSuchMethodError=main

Inside Inner classes we can't declare static members hence it is not possible to declare main method and hence we can't invoke inner class directly from command prompt.

Example:


Accessing Inner class code from outside of outer class:
Example:



Example:


Example:


According Inner class code:
From static area of outer class from Instance area of outer class
From outside of outer class Inner i=new Inner();
Outer o=new outer();
i.m1();
Outer.Inner i=o.new Inner(); outer o=new outer();
i.m1();

From The inner class we can access all member of outer class (both static and non-static) directly
Example:


Output:
10
20

With in the Inner class this always pointing to convert to current Inner class object.
To refer current outer class object we have to use "Outerclassname" .
"outer classname.this"
Example:


For the outer classes (Top Level classes) the applicable modifiers are public, default, final, abstract, strictfp.
But for the Inner classes in addition to above the following modifiers are also applicable.
public private
Default + protected=Inner classes
Final static
Abstract
Strictfp
2) Method Local Inner Classes:
Some times we can declare classes inside a method such type of class are called method local Inner classes.
The main purpose of method local Inner classes is to define method specific functionality.
The scope of the methods local Inner class is the method in which we declared it that is from outside of the methods we can't access methods local inner classes
As the scope is very less, this type of Inner classes are most rarely used inner classes
Example:


Output:
sum is 30
sum is 300
sum is 3000
sum is 30000
We can declare Inner class either in instance method or in static method.
If we declare Inner class inside instance method then we can access both static and non-static variable of outerclass directly from that Inner class.
If we declare Innerclass inside the static method then we canaccess only static members of outerclass directly from that innerclass
Consider the following code
Class Test
{
int x=10;
Static int y=20;
Public void m1()
{
int i=80;
Final int i=40;
class Inner
{
public void m2()
{
Line(1)
}
}
}
}
In line(1) which variable we can access
1) x //write
2) y //write
3) I //wrong
4) j //write

Note:
If declare m1() as static then at line(1) which variables

We can access y,j;
3) If we declare m2() as static ,then which variable we can access line(1) we will get Compile Error because Inside Inner classes we can't have static declarations.
The only applicable modifiers for methods local Inner classes are final, abstract, strictfp.

Anonymous Inner class:
Sometimes we can declare a class without name also such type of normal Inner classes are called Anonymous Inner classes
This type of Inner classes are most commonly used type of Inner classes
-There are 3 types of Anonymous Inner classes:
1. Anonymous Inner class that extends a class
2. Anonymous Inner class that implements an interface
3. Anonymous Inner class that defined inside methods arguments

a) Anonymous Inner class that extends a class:



Note:
1) The internal class name generated for anonymous Inner class is "Test t1.class".
2) Parent class reference can be used to hold child class object but by using reference.
We can call only methods available in the parent class and we can't call child specified methods.
In the anonymous inner classes also we can define new methods but we can't call these method form outside of the class because, we are depending on parent reference. This methods for internal purpose only.
Analysis:
Popcorn p=new popcorn();
Just we are creating an object of popcorn class
Popcorn p=new popcorn()
{
}

We are creating child class for the popcorn and for that child class we are creating an object with parent reference.


In the above example both main and child threads will be executed simultaneously and hence we can't exact output

b) Annonymous Inner Class That implements an Interface:


c) Annonymous Inner class that define Inside the method arguments:


Genaral class Vs Anonymous Inner class:
1) A general class can extend only one class.
2) Ageneral class can implement no of interface where as anonymous inner class can implements only one interface at a time.
3) A general class can extends another clas and can implement an interface simulteneosly,Where as anonymous inner class can extend another or implemets interface but not both simultaneously.
Static nested classes
 Sometimes we can declare inner class with static modifier such type of inner class called "static nested classes" .
 In the normal inner class,inner class object always associated with outer class object.
 That is without existing outer class object there no chance of existing inner class object.But static nested class object is not associated with outer class object there may be a chance of existing static nested class object
Example:


Within the static nested class we can declare static member including main() also. Hence it is possible to invoke nested class directly from command prompt.
Example:


Difference between Normal inner class and static Nested class:

1) Inner class object is always associated with outer class object. That is without existing outer class object there is no chance of existing inner class object
Static nested class object is not associated with outer class object. That is without existing outer class object there may be chance of existing static nested class object
2) Inside Normal inner class we can't declare static members.
Inside static nested class we candeclare static members.
3) Inside Normal inner class we can't declare main() and hence it is not possible to invoke inner class directly from command prompt.
Inside static nested class we can declare main() and hence we can invoke static nested class directly from command prompt.