Definition of Inner Class
A class that is present inside another class or block and is treated as one of its members is known as an inner class. It is also known as a nested class and can easily access all the members of the outer class/block. This helps one to declare a user-defined data type local to a block to implement a special type of relationship and also helps to write more readable and maintainable code. It can be classified into 2 different categories:-
- Static Nested Classes: If a nested class is declared as static.
- Inner Classes: Non-static nested classes is known as inner class.
Why do We Need Inner Class?
One needs to use inner class in the application when:-
- Local Class: If a class is used by only one other class then it is more preferable to put the single use class as a local inner class. Such embedding of a class with its helper class results into a better streamlined package.
- Optimized Code: Such arrangement of code makes our code more optimized and more efficient memory usage wise.
- Readable and Maintainable Code: Such arrangement of classes where class is placed closer to the place where it is used results in code that is easily readable and looks maintained.
How does Inner Class Work in Programming languages?
Let’s understand each of the above one by one:-
1. Static Nested
This type of static class is declared inside another class and is treated as one of its member. Since it is static thus can be easily called by other static members without initiating the outer class. And being static it does not have access to other non-static instance members of outer class.
Syntax:
class Outer{
public static class Inner{
}
}
2. Method Local
This is a type of class that is declared inside a method and is treated similar to its local methods and variables. And since it is a local member thus can not be accessed outside the method. To use it one needs to create its instance inside the method itself.
Note: Here, instance variables of Outer class are not accessible until they are declared as final upto JDk 1.7. In JDK 1.8 this flaw was overcome.
Syntax:
class Outer{
public int method1(){
//task to be performed
public class Inner{
//variables and methods of inner class
}
}
}
4.6 (3,144 ratings)
View Course
3. Anonymous
This type of classes are declared without a name . Such classes are used when one needs to override a method of a class or an interface. In this case declaration of the class and instantiation needs to be implemented at the same time.
Syntax:
AnonymousInner an_inner = new AnonymousInner() {
public void my_method() {
........
........
}
};
4. Inner Class
This type of classes are implemented by declaring a class inside another class where the inner class has access to all the instance variables and methods of the outer class. Since inner class here is associated with an instance thus must never be define static members.
Syntax:
class Outer{
class Inner{
}
}
Examples of Inner Class
Following are the example are given below:
Example #1 – Static Nested
Code:
package Try;
public class Office {
static class Admin {
public void my_method() {
System.out.println("Admin name is Sarthak");
}
}
public static void main(String args[]) {
Office.Admin nested = new Office.Admin();
nested.my_method();
}
}
Output:
Example #2 – Method Local
Code:
package Try;
public class Office {
void my_Method() {
final int numOfDesk = 23;
class Inner {
public void print() {
System.out.println("Number of desk in Office = "+ numOfDesk);
}
}
Inner inner = new Inner();
inner.print();
}
public static void main(String args[]) {
Office outer = new Office();
outer.my_Method();
}
}
Output:
Example #3 – Anonymous
Code:
package Try;
abstract class MyInner {
public abstract void myMethod();
}
public class Office {
public static void main(String args[]) {
MyInner my_inner = new MyInner() {
public void myMethod() {
System.out.println(“Implementing myMethod named abstract method");
}
};
my_inner.myMethod();
}
}
Output:
Example #4 – Inner Class
Code:
package Try;
class Company {
class MyCEO {
public void show() {
System.out.println("CEO of this institute is Mr. Rakesh Kumar");
}
}
}
public class Office {
public static void main(String[] args) {
Company.MyCEO in = new Company().new MyCEO();
in.show();
}
}
Output:
Advantages
- Maintainable Code: It places one time use class inside the other class that enhances the readability and maintenance of the code thus our application becomes more maintainable.
- Enhances Encapsulation: Encapsulation is one of the property of Object Oriented Programming Languages where related variables and methods are encapsulated under one block named as class and are referred using instance of that class. Since inner class is implemented as a part of the outer class and is treated as a member of the class thus enhances encapsulation in our code thus making it more efficient and maintainable.
- Improves Optimization: Using this in one’s application helps the compiler to easily compile the class thus enhances the optimization of the code and also memory and CPU utilization by the compiler is reduced as less memory is utilized for a new class.
Conclusion
Inner or nested class is a way out to place the classes that are used only at one place in out code closer. This enhances the encapsulation and optimization of the code and make our code more readable and maintainable as the members are made more accessible from other classes.
Recommended Articles
This is a guide to Inner Class. Here we also discuss how does inner class work in programming languages? along with different examples and its code implementation. you may also have a look at the following articles to learn more –