Friday, September 25, 2009

Notes About Inner Class - Core Java

1. Innerclass nested with other class i.e. enclosing class. They are four types:
i. Static
ii. Member class
iii. Local class
iv. Anonymous Class

N.B - Inner class is a member of enclosing class like method or field.

2. Static Member Class/Interface:
i. Static class is a static member of class like static method/field.
ii. Interfaces can be defined as static member class.
iii. A static member class is not associated with any instance of the containing class (i.e., there is no this object).
iv. A static member class has access to all static members of its containing class, including private members. The reverse is true as well: the methods of the containing class have access to all members of a static member class, including the private members. A static member class even has access to all the members of any other static member classes, including the private members of those classes.
v. A static member class cannot have the same name as any of its enclosing classes. In addition, static member classes and interfaces can be defined only within top-level classes and other static member classes and interfaces.

5. Member Class

i.Member class is a member (instance) of the enclosing class and has access to any and all methods or variables of enclosing one and even the parent's this reference.
ii. A member class cannot have the same name as any containing class or package.
iii. Member classes cannot contain any static fields, methods, or classes (with the exception of constant fields declared both static and final). The reason of it as static fields, methods, and classes are top-level constructs not associated with any particular object, while every member class is associated with an instance of its enclosing class. Defining a static top-level member within a non-top-level member
class simply promotes confusion and bad programming style, so you are required to define all static members within a top-level or static member class or interface.
iv. Interfaces cannot be defined as member classes. An interface cannot be instantiated, so there is no object to associate with an instance of the enclosing class. If you declare an interface as a member of a class, the interface is implicitly static, making it a static member class.
v. To access the this reference of the containing class use classname.this

6. Local classes are declared within a block of code and are visible only within that block, just as any other method variable.

7. An anonymous class is a local class that has no name.

8. Advantages of inner classes can be an object-oriented advantage, an organizational advantage, and a call-back advantage.

i. Lets prove the above three advantages:

Object-Oriented
---------------
Let's look at the member class. Since its instance is a member of its parent instance, it has access to every member and method in the parent. At first glance, this might not seem like much; we already have that sort of access from within a method in the parent class. However, the member class allows us to take logic out of the parent and objectify it.

Organizational
--------------

Inner classes allow us to further organize our package structure through the use of namespaces. Instead of dumping everything in a flat package, classes can be further nested within classes. Explicitly, without inner classes, we were limited to the following hierarchy structure

Callback
--------

Inner member classes and anonymous classes both provide a convenient method for defining callbacks. The most obvious example relates to GUI code.

As with anything else, you have to take the good with the bad. Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code. Moreover, from a development point of view, most Java tools come up a bit short on their support of inner classes.

Ref: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_10.htm

No comments:

Post a Comment