Nested Classes Java

Nested Classes Java


JDK 1.1 introduced a new type of classes known as inner classes. All the classes you have done so far are known as top-level classes. A class written inside another class is known as "inner class" or "nested class". That is, an inner class is enclosed within another class. Inner classes give more accessibility and scope restrictions for other classes.

The class which encloses is known as outer class and getting enclosed is known as inner class. Outer class is known as top-level class. The classes of a package are all top-level classes.

Types of Inner Classes (Nested Classes Java)

There are four styles of creating an inner class.

1. Static member classes
2. Member classes
3. Local classes
4. Anonymous classes

All the above are discussed very clearly in Inner Classes and is advised to read before proceeding further.

Where nested classes are useful? Does nested class has any advantage?

1. Nested are used for brevity of code.
2. Nested class does not need any additional or separate file to place.
3. Nested classes (or inner classes) are comparatively small and tightly coupled with the “parent” class or “outer” class. Avoids lot of helper classes and fits to explain the OOD priciple of containment.
4. All the code in the same file increases readability and increases performance (like online code). Their importance is growing in the coding and treated as a good practice.

Disadvantages of inner classes

1. Inner classes is mostly a matter of style. There will be high level cohesion between inner class and outer class. If the inner class grows inot a complex code of its own, then better move the inner class into its own (to write a separate class in a separate file).

Nested classes in real life usage?

1. Nested classes implementation as adapter classes are widely used.
2. Anonymous inner class is a special case of nested classes.
3. When building a complex Calendar with one or more Special Events, someone would be inclined to nest Special Events within the Calendar object, effectively encapsulating it. To this extent, one could argue that nesting classes is the best way to organize a file system.
4. A simple example to use nested class is, a nested concrete class implements Action Listener that being used in the parent class for a component.
5. In general we can call nested inner class as Helper class for outer class. We can add this class to help outer class to extend some functionality within. For example Login class may have Authentication class as nested one.

Because inner classes usage is growing slowly, they are discussed more elaborately here. Android (Java based Mobile OS developed by Google) uses inner classes very extensively, infact anonymous inner classes are passed as parameters to methods.

In this series following combinations are discussed.

1. Java Nested Classes
2. Static Nested Classes
3. Nested Interfaces
4. Nested Interface within Class
5. Interface inside interface
6. Class inside interface

Note: It is advised to read the basics and types of inner classes before proceeding further.

Now let us go on the first variation – Java Nested Classes

Outer1 o1 = new Outer1();

Outer1, being top-level class (other one is nested which cannot exist on its own. To reach inner (or nested), it is necessary to go through outer), can exist by itself. We can create an object of it directly.

Outer1.Inner1 i1 = o1.new Inner1();

Inner1 is an inner class. It cannot exist by itself and is part of another class. It must be accessed through outer class, even when an object is created.

Inner1 i3 = new Outer1().new Inner1();

It looks strange as i3 is declared without the help (no reference) of Outer1. Because inner class is part of outer class, inner class can access outer class and its members. Inner class behaves as if outer class is of its own (something, is-a type of relationship, I assume).

System.out.println(i1.x);

The above statement raises compilation error. If the inner class would like to access the outer class variable x, it must come through a method call as follows.

No way for outer class to access inner class variable y. Inner class can access outer class but outer class cannot access inner class. In real life also it is the same. A son is contained within a father. Son can go to father’s property but father cannot come to son’s property.

In the above example, inner class is not static and no variable is static. Let us see this variation in the next program – Java Static Inner Class.

Leave a Reply

Your email address will not be published. Required fields are marked *