Thursday, March 12, 2009

What are the different inner classes available in java? Explain each inner

Question :What are the different inner classes available in java? Explain each inner
class with an example. (CoreJava)

Answer :There are four types of inner classes in java
1. Member class
2. Static member class
3. Local class
4. Anonymous class
1. Member class
A member class is defined as a member of a class. The member class is
instance specific and has access to any and all
methods and members, even the parent's "this" reference. All public,
protected, default, and private members are visible
to instances of member class.
You must provide an instance of the enclosing class when you create a new
instance of member class.
public class EnclosingClass {
private int instVar = 1;
public class MemberClass {
public void innerMethod () {
instVar++;
}
}
public MemberClass createMember () {
return this.new MemberClass ();
}
}
If you need to create an instance of member class outside of the scope of
the enclosing class, you need to use an
instance of the enclosing class to create the member instance:
EnclosingClass ec = new EnclosingClass ();
EnclosingClass.MemberClass mc = ec.new MemberClass ();
or
EnclosingClass.MemberClass mc = new EnclosingClass ().new
MemberClass ();
member classes can be declared as abstract and final. The access
specifiers, public, protected, default and private
can be used within the class.
2. Static member class
A static member class is a static member of a class. Like any other static
method, a static member class has access to
all static methods of the parent, or top-level, class. These classes can use
instance variables and methods only
through an object reference. Only public, final, and static are permitted as
modifier inside a static member class.
E.g.
public class EnclosingClass {
private static int static_var = 0; // Has got access
public int instance_var = 0; // Has got no access
public static class StaticInnerClass {
}
}
Because the inner class is static, it can access only the static_var variable,
even though it is private
It cannot access the instance_var variable because it is not static,
regardless of the fact that it is public
The fully qualified class name for the inner class is
EnclosingClass.StaticInnerClass
These classes can be declared as public, abstract and final.
public class Outer {
public String name = "Outer";
public static void main (String argv[]) {
Inner i = new Inner ();
i.showName ();
} //End of main
private static class Inner {
String name = new String ("Inner");
void showName () {
System.out.println (name);
}
} //End of Inner class
}
3. Local class
Local classes are declared within a block of code and are visible only within
that block, just as any other method
variable. Local classes are good way to maintain Encapsulation.
Local classes, like local variables, cannot be declared public, protected,
private, or static.
Local classes cannot have static members.
Local classes can only access final local variables and method arguments of
the enclosing method.
Local inner classes can be declared as abstract and final.
Example
public class AnyClass {
void localClassDemo() { // a function
class LocalClass { // a class inside a function definition
void func() {
System.out.println( “in Local class”);
}
}
LocalClass local = new LocalClass();
local.func() ;
}
}
4. Anonymous class
An anonymous class is a local class that has no name. An anonymous class
is implicitly final.
Anonymous classes cannot be public, protected, private, or static. The
syntax for anonymous inner classes does not
allow for any modifiers to be used. An anonymous inner class can extend a
superclass or it can implement an interface.
But not both.
Example
public class SomeGUI extends JFrame
{
protected void buildGUI()
{
button1 = new JButton();
button2 = new JButton();
button1.addActionListener(
new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
// do something
}
});
}
}

No comments: