An inner class from its own perspective is nothing but a class that has been defined inside
another class. For example
[Example 1]
class Outer
{
class Inner
{
}
}
|
The most interesting thing about them is that they are not only visible inside the
class but with the use of proper access modifier we can allow them to be used
inside/outside the class and inside/outside the package. There are a couple of
points that are common between different types of inner classes (Described
later) so it’s a good point to discuss them.
There is no relationship (Inheritance) between Outer and Inner class, until and
unless you explicitly specify one.
An inner class can access all the private resources (Methods/Properties) of
the Outer class.
Now as we know what are they, let’s see what are different types of inner classes available (Yeah there are different types of inner classes). The type not only specifies the syntax but also specifies its usage restriction.
Type 1 [Defining an inner class inside a class]
This is the standard type (see example1) where we can specify a class inside another class. We will be
dealing mostly with these types in this article. The following points need to be considered:
Access to Inner class is available inside the class.
The access modifier used will decide availability of Inner class, outside the class/package.
Type 2 [Defining an inner class inside a method]
This type specifies the definition of an inner class inside a method such as
[Example 2]
class Outer
{
public void SomeMethod()
{
class Inner
{
}
}
}
|
The point of consideration here is, that the existence of class Inner is only
available when we call SomeMethod(). This point specifies the following issues
class Inner is not available outside the method SomeMethod() even inside class
Outer.
You can not specify an access modifier with a class of this type.
The first point is self-explanatory but the later requires a little detail. An
access modifier specifies the rights to a class/object. With this type of inner
class the method SomeMethod() specifies the boundaries (scope) as it specifies
for any ordinary object. With the concept of scope around there is no point to
further restrict or elaborate the restriction rights by specifying an access
modifier.
Type 3 [Defining an inner class inside a method-Anonymous Classes]
This is basically an enhanced version of Type-2 class. An Anonymous class as the
name suggests doesn’t have a name, which creates some suspicious opinions regarding
this article; but before I deal with them, consider the following example
[Example 3]
class MyFrame extends Frame
{
Button m_btn=new Button(“Click Me”);
public MyFrame()
{
//… After some initialization
add(m_btn);
m_btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
}
|
At this point it does appear that at addActionListener something really
obnoxious has been done but in fact this is our anonymous class. Let’s take a
brief tour with it. The first point that is necessary to understand about this
type of inner class is that they don’t have any usual declarations like
[AccessModifer] class [ClassName]
Instead they begin with an impression that we would like to create a temporary
instance of any class. Next we specify the base class/interface which acts as
the parent class for our anonymous class (In Example3 we have specified that our
anonymous class will implement the interface ActionListener). Following are the
points that do require attention
It must either extend a class or implement an interface but it can’t do both
like a usual inner class.
You can’t specify an access modifier.
You can’t specify a constructor. Why?? well simple, they don’t have any names.
If you would like to call a different constructor of the base class then you can
simply specify it at the creation time. For example
SomeObj.SomeMethod(new SomeClass(45)
{
//Your anonymous class extending from SomeClass
});
|
An anonymous class can only access final variables of the method in which it was
specified. |