nested class and inner class in Java
Nested classes :
It is possible to define a class within another class; such classes are known as nested classes.
There are two types of nested classes :
A. static nested class
B. non-static (inner) class
A. Static nested class :
A static class created within class.
These are the static members of a class.
Syntax :
class Outer
{
static class inner
{ //code
}
}
Example :
class outer
{
static class inner
{
void show()
{
System.out.println("static nested class ");
}
}
}
public class Test
{
public static void main(String args[])
{
outer.inner i=new outer.inner();
i.show();
}
}
Output :
static nested class
B. Non-static nested class (inner class) :
Inner class is a non-static nested class.
It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.
Advantages of java inner classes:
They are as follows:
1. Nested classes represent a special type of relationship that is it can access all the members of outer class including private data members and methods.
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.
4. It increases encapsulation and security.
There are 3 types of inner classes :
They are as follows:
1. Member inner class
2. Anonymous inner class
3. Local inner class
1.member inner class :
A class created within class and outside method.
Syntax :
class outer {
class Inner
{
//code
}
}
Example :
class outer
{
class inner
{
void show()
{
System.out.println("member inner class");
}
}
}
public class Test
{
public static void main(String args[])
{
outer o=new outer();
outer.inner i=o.new inner();
i.show();
}
}
Or
public static void main(String args[])
{
outer.inner i=new outer().new inner();
i.show();
}
Output :
member inner class
2.Anonymous inner class :
A class with no name is known as an anonymous inner class.
It is created for implementing interface or extending class. Its name is decided by the java compiler.An inner class declared without a class name is known as an anonymous inner class.
They are used for override the methods of class or interface.
Syntax :
AnonymousInner o = new AnonymousInner()
{ public void my_method()
{ ........ ........ }
};
a. it is created for extending class.
Example :
abstract class A
{
abstract void show();
}
public class Test
{
public static void main(String args[])
{
A o =new A()
{
public void show()
{
System.out.println("Hello");
}
};
o.show();
}
}
Output :
Hello
Or (method overriding)
Example :
class A
{
public void show()
{
System.out.println("Hi");
}
}
public class Test
{
public static void main(String args[])
{
A o =new A()
{
public void show()
{
System.out.println("Hello");
}
};
o.show();
}
}
Output :Hello
b. It is created for implementing interfaces.
Example :
interface A
{
void show();
}
public class Test
{
public static void main(String args[])
{
A o =new A()
{
public void show()
{
System.out.println("Hello");
}
};
o.show();
}
}
Output :
Hello
3 . local inner class :
A class created within method.
A class can also created within block or constructor.
Syntax :
class outer
{
type method(parameter-list)
{
class inner
{
//code
}
}
}
Example :
class outer
{
void display()
{
class inner
{
void show()
{
System.out.println("local inner class ");
}
}
inner i=new inner();
i.show();
}
}
public class Test
{
public static void main(String args[])
{
outer o =new outer();
o.display();
}
}
Output :
local inner class
Advantage:
We can declare multiple classes with with same name within the method or block.
Example :
Class outer
{
void display ()
{
class inner
{
void show() {}
}
class inner
{
void msg() {}
}
}
}
Limitations :
-Scope(access) of local inner class within method, block or constructor only.
Comments
Post a Comment