The main reason behind the use of inner class is to group the classes with particular logic. This process of grouping classes is called as encapsulation. Increased use of encapsulation in a program will make the code more readable.
Why the Use of Inner Class is Necessary in Java?
Consider you are using a chat window based on Java Graphical User Interface (GUI). To program this chat GUI; a developer will need a method which reads the text client window and transmit it to the user. Similarly the user message is transmitted to the client using the same method. If you are wondering how these methods will be called? The answer is simple, pressing of Enter key is used as a trigger to call these methods.
- SEE: Java interview Q&A available here
In the above example two methods are required to transmit the chat message. The methods used to transmit the message are
- Method used to read the chat message and transmit it.
- Method used to handle the above method and trigger it. i.e. Pressing of “Enter” key.
If the methods mentioned above are declared using separate classes, the trigger method and chat method are separated which results in creating separate instance for every message. In real world each and every message will be opened in a new window.
Why Message and Trigger Methods are Not Combined?
Combining the methods used for message and trigger under the same class may also eliminate the need of inner class. This may look like a bright idea at first but take a scenario in which you are chatting with multiple persons. If both the methods are combined, the trigger method will be activated to all the chat windows and whatever message you were typing will be transmitted to everyone.
If inner classes are used in the “multiple chat scenario” only message you typed for a particular person will be transmitted because the trigger is independent between the chat windows. In other words, “one class cannot be inherited from two different classes”.
Can we declare inner classes as final?
The use of the keyword static is only allowed in inner class. If a class is declared as final, then it becomes constant and cannot be changed in future. Classes which are constant cannot be inherited because it cannot be changed in future. It is possible to declare inner classes as final, if no inheritance of that particular class is required by the outer class.
Does Java allow extension of inner class?
The extension of both inner and outer class is allowed by Java. Consider the following program which has a nested class and an inner class.
public class OutClass
{
static class StaticNestClass
{
void show()
{
System.out.println(“Inside the StaticNestClass”);
}
}
class InnClass
{
void show()
{
System.out.println(“Inside the InnClass”);
}
}
}
The following code is used to extend the outer class with respective to inner and nested class.
public class OutClassDeriveClass extends OutClass
{
static class StaticNestDeriveClass extends OutClass.StaticNestClass
{
@Override
void show()
{
System.out.println(“Inside the StaticNestDeriveClass”);
}
}
class InnDeriveClass extends OutClass.InnClass
{
@Override
void show()
{
System.out.println(“Inside the InnDeriveClass”);
}
}
}
Now we can test the class extension using the code given below.
public class InheritanTest1
{
public static void main(String[] args)
{
OutClass outClass= new OutClass();
OutClass.InnClass innClass = outClass.new InnClass();
innClass.show();
OutClass.StaticNesteClass staticNesteClass = new OutClass.StaticNesteClass();
staticNesteClass.show();
OutClassDeriveClass outClassDeriveClass = new OutClassDeriveClass();
OutClassDeriveClass.InnDeriveClass innDeriveClass=outClassDeriveClass.new InnDeriveClass();
innDeriveClass.show();
OutClassDeriveClass.StaticNestDeriveClass staticNestDeriveClass = new OutClassDeriveClass.StaticNestDeriveClass();
staticNestDeriveClass.show();
}
}
- Download: Final year projects for free
The Output for the program will be
Inside InnClass
Inside StaticNesteClass
Inside InnDeriveClass
Inside StaticNestDeriveClass
Does the use of inner classes defy the process of encapsulation?
Most novice Java programmers believe that use of inner class will defy the process of encapsulation. Inner class is a process of creating a class within an outer class by the same person who coded the outer class. Creation of inner class is an intentional design by the programmer for various reasons.
Encapsulation is the process of hiding certain classes from an user to restrict his/her operations. Encapsulation is done by another user who wishes to restrict other programmers. While inner class is created by the same user to prevent inheritance of that particular class.