Inner class in a class defined as a class inside another class. So it is itself a class but first it will act as a member of that class. As we know that we cannot make a class as static but inner class can be made as static which proves that it is first a member of a class in which it is defined . We can execute our class with its name but to execute a inner class we need to take help of outer class that proves that an inner class has identity with its outer class.
Important points regarding Inner Class :
1. A .class file is generated for inner class but the file name will be as follows :
< outer_class_name > $ < inner_class_name > . class
2. We can be able to create a reference of inner class within the scope of an outer class simply as a normal reference definition.
3. To create a reference of inner class outside the scope of an outer class then we need to indicate to write the inner class name followed by outer class name, like this
< outer_class_name > . < inner_class_name > . < ref_value > = null ;
4. As inner class itself is a member of a outer class then it the other members won't be the member of the inner class.
5. We can have a class inside an inner class which is defined as the inner class of inner class.So like this we can have infinite number of inner class / nested classes.
6. Inner class is generally used for abstraction.
Example of an inner class :-
class prog { // main class
int x;
class A { // Inner class of prog
int x ;
String name ;
public static void main( String sush[] ) {
System.out.println(" Inner class " ) ;
A a = new A(); // Object of an inner class
}
}
In the above example class A is an inner class of prog class which is also called as the instance class.
Types of Inner class :
There are four (4) types of inner class :
1. Instance inner class. --> Click here to know more about instance inner class
2. Static inner class. --> Click here to know more about Static inner class
3. Local Inner class --> Click here to know more about Local inner class
4. Anonymous inner class.
0 comments:
Post a Comment