0% found this document useful (0 votes)
2 views

INNER Class

Inner classes are classes defined within another class, introduced to resolve GUI bugs and now widely used in programming. They are categorized into four types: Normal inner classes, Method local inner classes, Anonymous inner classes, and Static nested classes, each with distinct access rules and use cases. Inner classes have a Has-A relationship with their outer class, and their accessibility varies based on their type and the context in which they are declared.

Uploaded by

uddeshtemp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

INNER Class

Inner classes are classes defined within another class, introduced to resolve GUI bugs and now widely used in programming. They are categorized into four types: Normal inner classes, Method local inner classes, Anonymous inner classes, and Static nested classes, each with distinct access rules and use cases. Inner classes have a Has-A relationship with their outer class, and their accessibility varies based on their type and the context in which they are declared.

Uploaded by

uddeshtemp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

INNER Class

 Sometimes we can declare a class inside another class such type of classes is called Inner Classes.
 Sun people introduced inner classes in 1.1 version as part of "Event Handling" to resolve GUI bugs.
 But because of powerful features and benefits of inner classes slowly the programmers start using in
regular coding also.
 Without existing one type of objects if there is no chance of existing another type of object then we
should go for inner classes.
Ex.: - Without existing University object there is no chance of existing Department object hence we
have to define Department class inside University class.

NOTE :- *Without existing Outer Class there is no chance of existing Inner class objects.
*The relationship between outer class and inner class is not IS-A relationship and it is Has-A
relationship.
Based on the purpose and position of declaration all inner classes are divided into 4
types.
They are:
1. Normal or Regular inner classes
2. Method Local inner classes
3. Anonymous inner classes
4. Static nested classes.
1. Normal or Regular Inner Class
If we are declaring any named class inside another class directly without static modifier such type of inner
classes are called normal or regular inner classes.
Example: class Outer {
class Inner {
}
}

Example: class Outer {


class Inner {
}
public static void main(String[] args) {
System.out.println("outer class main method");
}
}
Output :-
 Inside inner class we can’t declare static members. Hence it is not possible to declare main() method and
we can’t invoke inner class directly from the command prompt.
Example: class Outer {
class Inner {
public static void main(String[] args) {
System.out.println("inner class main method");
}
}
}
Output :- inner classes cannot have static declarations public static void main(String[] args)
Accessing inner class code from static area of outer class :
Ex: - class outer{
Class Inner{
public void methodOne(){
System.out.println(“inner class method”);
}
}
Public static void main (String[] args){
}
}
Accessing inner class code from instance area of outer class:
Ex :- class outer{
class Inner {
public void methodOne() {
System.out.println("inner class method");
}
}
public void methodTwo() {
Inner i=new Inner();
i.methodOne();
}
public static void main(String[] args) {
Outer o=new Outer();
o.methodTwo();
}
}
Accessing inner class code from outside of outer class:
Ex:- class Outer {
class Inner{
public void methodOne() {
System.out.println("inner class method");
}
}
}
class Test{
public static void main(String[] args) {
new Outer().new Inner().methodOne();
}
}
EX:- Tell output of this
class Outer {
int x=10;
class Inner {
int x=100;
public void methodOne() {
int x=1000;
System.out.println(x);
System.out.println(this.x);
System.out.println(Outer.this.x);
}
}
public static void main(String[] args) {
new Outer().new Inner().methodOne();
}
}
The applicable modifiers for outer classes are:
1. public
2. default
3. final
4. abstract
5. strictfp
But for the inner classes in addition to this the following modifiers also allowed.
Inner Class :- public, default, final, abstract, strictfp, private, protected, abstracts
2. Method local Inner Class
 Sometimes we can declare a class inside a method such type of inner classes are called method local
inner classes.
 We can access method local inner class only within the method where we declared it. That is from
outside of the method we can't access. As the scope of method local inner classes is very less, this type of
inner classes are most rarely used type of inner classes.

Ex:- class Test {


public void methodOne() {
class Inner {
public void sum(int i,int j) {
System.out.println("The sum:"+(i+j));
}
}
Inner i=new Inner();
i.sum(10,20);
i.sum(100,200);
i.sum(1000,2000);
}
public static void main(String[] args) {
new Test().methodOne();
}
}
Output: The sum: 30
The sum: 300
The sum: 3000

 If we are declaring inner class inside instance method then we can access both static and non static
members of outer class directly.
 But if we are declaring inner class inside static method then we can access only static members of outer
class directly and we can't access instance members directly.
 From method local inner class we can't access local variables of the method in which we declared it.
But if that local variable is declared as final then we won't get any compile time error
class Test {
int x=10;
public void methodOne() {
int y=20;
class Inner {
public void methodTwo() {
System.out.println(x);
System.out.println(y);
}
}
Inner i=new Inner();
i.methodTwo();
}
public static void main(String[] args) {
new Test().methodOne();
}
}
3. Anonymous Inner Classes
 Sometimes we can declare inner class without name such type of inner classes are called anonymous
inner classes.
 The main objective of anonymous inner classes is "just for instant use".

classPopCorn {
public void taste() {
System.out.println("spicy");
}
}
class Test {
public static void main(String[] args) {
PopCorn p=new PopCorn() {
public void taste() {
System.out.println("salty");
}
};
p.taste();//salty
PopCorn p1=new PopCorn()
p1.taste();//spicy
}
}

1. We are creating child class for PopCorn without name.


2. We are overriding taste() method.
3. We are creating object for that child class with parent reference.

Note: Inside Anonymous inner classes we can take or declare new methods but outside of anonymous
inner classes we can't call these methods directly because we are depending on parent reference. [parent
reference can be used to hold child class object but by using that reference we can't call child specific
methods]. These methods just for internal purpose only.

4. Static nested Classes


 Sometimes we can declare inner classes with static modifier such type of inner classes are called static
nested classes.
 In the case of normal or regular inner classes without existing outer class object there is no chance of
existing inner class object.
i.e., inner class object is always strongly associated with outer class object.
 But in the case of static nested class without existing outer class object there may be a chance of existing
static nested class object.
i.e., static nested class object is not strongly associated with outer class object.

class Test {
static class Nested{
public void methodOne() {
System.out.println("nested class method");
}
}
public static void main(String[] args){
Test.Nested t=new Test.Nested();
t.methodOne();
}
}
 Inside static nested classes we can declare static members including main() method also. Hence it is
possible to invoke static nested class directly from the command prompt.
 From the normal inner class we can access both static and non static members of outer class but from
static nested class we can access only static members of outer class.

You might also like