Sunday, November 26, 2006

Insight into Inner classes

Is there any advantages to Java's Inner Classes?
Believe me there are advantages of using java's inner classes.

But before going into details about advantages let's try to answer one question "what is Inner class?"
Inner class:
Declaration of a class within another class is called an "Inner Class".
It is basically a name of nested classes.
In other words..
A class that is declared within another class or interface, is called a nested class[inner class].


A normal class is a direct member of a package, a top-level class.
Inner classes, which became available with Java 1.1, come in four flavors:

  1. Static Member classes
  2. Member classes
  3. Local classes
  4. Anonymous classes
    Let's take quick look at each in turn..
Static Member classes/Interfaces: Like any other static method, a static member class has access to all static methods of the parent, or top-level class.
Since it is defined as "static" member of enclosing class it can be accessed or instantiated without any instance of an enclosing class.


Sample code:
class Outer {

static class Inner {
// Nested Top-level class or static nested class
}

}

How to access Inner class?
Outer.Inner theInner = new Outer.Inner();

Note: There is no such thing as an member inner interface. Because, Interface are always implicitly static. They are always top-level, not member inner.


Member classes: It is defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.


Sample Code:

class Outer{
class Inner{
// Member or Non-static inner class
}
}
How to instantiate member class?
Outer.Inner theInner = new Outer().new Inner();
or
Outer theOuter = new Outer();
Outer.Inner theInner = theOuter.new Inner();

Note: Member classes cannot have any static member unless it is a complie-time constant[final static field].

class Outer3{
class Inner3{
static final int x = 0;// allowed
// static int b = 0; // invalid
int j =0;// okay.
}
}
Local Classes: Local classes are declared within a block of code and are visible only within that block,just as any other method variable.


Sample code

class Outer{
void method(){
class Inner {

}
}
}
Anonymous Classes: Annoymous class are nothing but local classes which does not have name.


Sample Code:

void initUserInterface() {
//declaration
//...
addWindowListener(//
//class declaration
new WindowAdapter(){
//implementation for the method
public windowClosing(WindowEvent e){
System.exit(0);
}
}
}

Visit here to get more detail on Inner class
http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html
After giving brife discription about Inner classes,
Now its time to jump insight Inner classes

When you are designing a class hierarchy, you try to figure out the relationship between the classes that you decide upon. These relationships more often than not can be classified as "is a" relationship and "has a" relationship.
The "is a" relationship leads to the inheritance hierarchies, as the clause suggests, the derived class "is a" kind of super class.

click here for example : url http://www.developer.com/java/article.php/899361

The other relationship in object-oriented design is the "has a" relationship. This containership design is perhaps a more-used design strategy than the inheritance one.
click here for example : url http://www.developer.com/java/article.php/899361

There is, however, a third, less obvious, still fairly used relationship between classes, I call it the "facet" relationship. For this type of relationship Inner classes comes into picture.Suppose there is a requirement (by an automobile design program) to have access to all the different types of bolts used in the automobile. Now, there could be various types of bolts used, with different dimensions, size, number of threads, etc., and we need to iterate over them to either find ones that suit our requirement, of say size, or perform some calculation of, say, finding the median weight of all the bolts.

What we want is an "iterator" over the bolts. Inner classes are most suited for this kind of requirement. We define an Iterator class inside the class Automobile and return a reference to it through a public method bolIterator(). Better still, we may make the inner class anonymous, the idea is that the "name" of this inner class is going to be used only once (when its object is created in the iterator() method of the enclosing class), so we might do away with the name.

import java.util.Iterator;
class Automobile {
Bolt[ ] boltArray;
......
......
public Iterator boltIterator() {
return new Iterator () {
int count;
public boolean hasNext() {
return (count <>

Here, the methods next() and hasNext() of the interface Iterator may access the private members of the class Automobile. Note here that the class Automobile has nothing to do with this behavior of Iterator. All it does is provide a method that hands over an Object of type Iterator through which the rest of the world can iterate over the bolts in Automobile. The Iterator here is not a behavior of the class Automobile but a "facet" of it.




Resources:

For Inner class usage example

http://www.developer.com/java/ent/article.php/899361http://www.developer.com/java/ent/article.php/903361

http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html

http://www.javaworld.com/javaworld/javatips/jw-javatip75.html?page=1

http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index.html




No comments: