Inner Class In Java

 Inner Class In Java

===============================================================

Sometime we declare a class inside another class, such type of classes are called Inner Class.

Inner class concept introduce in 1.1v to fix GUI bugs as the part of event handling. But because of powerful feature and benefits of inner classes, slowing programmers started using in regular coding also.

Without existing one type of object if there is no  chance of existing  another type of object then we should go for Inner Class.

Ex 1.

    University consist of several departments without existing university there is no chance of department. Hence we have to declare Department class inside University class.


Ex 2.

      Without existing Car object there is no chance of existing Engine  object. Hence we have to declare Engine class inside  Car class.

 Ex 3.

Map is group of Key,Value pairs and each Key,Value pairs  called an Entry

Without existing Map object there is no chance of existing Entry object. Hence interface Entry is define inside Map interface.

Note: 

  1. Without existing outer class object there is no chance of existing inner class object.
  2. The relation between outer class and inner class is not IS-A relation but it is HAS-A relationship. (Composition/ Aggregation)

 

 Type of Inner class

==============================================

Based on position of declaration and behavior Inner class are divide into 4 types.

 

    1. Normal / Regular inner class.
    2. Method local inner class.
    3. Anonymous inner class.
    4. Static nested class. 
RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post