By,
Nikhil
In Java, just like methods, write a class inside another class is allowed in Java. The class written inside is called a inner class or nested class, and the class that holds the nested class is called the outer class.
The syntax to write a inner class is given below. Here the class Outer_class_Demo is the outer class and the class Inner_Demo is the nested class.
class Outer_class_Demo
{
class Nested_class_Demo
{
//functionality
}
}
Nested classes have two types:
• Non-static inner classes: non-static members of a class.
• Static inner classes: static members of a class.
Inner Classes (Non-static Nested Classes)
Inner classes provides a security mechanism in Java. if we have class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a other class.
Inner classes has three types depending on where and how you use them. i.e. as follows :
• Inner Class
• Method local Inner Classes
• Anonymous Inner Class
The syntax to write a inner class is given below. Here the class Outer_class_Demo is the outer class and the class Inner_Demo is the nested class.
class Outer_class_Demo
{
class Nested_class_Demo
{
//functionality
}
}
Nested classes have two types:
• Non-static inner classes: non-static members of a class.
• Static inner classes: static members of a class.
Inner Classes (Non-static Nested Classes)
Inner classes provides a security mechanism in Java. if we have class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a other class.
Inner classes has three types depending on where and how you use them. i.e. as follows :
• Inner Class
• Method local Inner Classes
• Anonymous Inner Class
1) Inner Class
Creating an inner class is quite simple. You just need to write a class within a class. An inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.
below is the program to create an inner class and access it.
class Outer_class_Demo
{
int num;
private class Inner_class _Demo
{
public void print_method()
{
System.out.println("This is an inner class");
}
}
void display_Inner()
{
Inner_class _Demo inner = new Inner_class _Demo();
inner.print_method ();
}
}
public class My_class
{
public static void main(String args[])
{
Outer_class _Demo obj = new Outer_class _Demo();
obj.display_Inner();
}
}
2) Method local Inner Class
We also can write a class within a method and this will be a local type In Java. As Like a local variables, the scope of the inner class is restricted within the method only it can’t be accessed outside of the that method .
Below program shows how to use a method local inner class in real time programming.
public class OuterclassDemo
{
void my_MethodDemo ()
{
int num = 23;
//method inner class
class MethodInner_Demo1
{
public void print()
{
System.out.println("This is method inner class "+num);
}
}
//Accessing
MethodInner_Demo1 inner = new MethodInner_Demo1();
inner.print();
}
public static void main(String args[])
{
OuterclassDemo outer = new OuterclassDemo ();
outer.my_MethodDemo ();
}
}
3)Anonymous Inner Class :-
An inner class declared or define without a name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time. Generally they are used whenever you need to override the method of a class or an interface.
Syntax of an anonymous inner class are as follows:
Anonymous_InnerClass obj = new Anonymous_InnerClass()
{
public void method1()
{
//Functionality;
}
};
No comments:
Post a Comment