please send code example of inner classes?

Answer Posted / shaik baji

In Java we have 4 types of inner classes

1)Regular Inner Classes
2)Method local Inner Classes
3)Static Inner Classes
4)Anonymous Inner Classes

Regular Inner Class:
---------------------

public class RegularInnerClassDemo
{
RegularInnerClassDemo()
{
System.out.println("RegularInnerClassDemo");
}
class InnerClass
{

InnerClass()
{
System.out.println("InnerClass");
}
public void go()
{
System.out.println("hi");
}
}
public static void main (String [] args)
{
RegularInnerClassDemo f = new
RegularInnerClassDemo();
f.makeBar();
}
void makeBar()
{
(new InnerClass() {}).go();
}
}

Method Local Inner Class:
--------------------------
public class MethodLocalInnerClassDemo
{
public static void main (String [] args)
{
class InnerClass
{
public String name;
public InnerClass(String s)
{
name = s;
}
}
Object obj = new InnerClass("Zippo");
InnerClass h = (InnerClass) obj;
System.out.println(h.name);
}
}

Anonymous Inner Class:
----------------------

Anonymous inner class comes in two forms

1)Normal Anonymous Inner Class
2)Parametrized Anonymous Inner Class

1) Normal Anonymous Inner Class:

Again the Normal Anonymous Inner class is two types

a)Extending the class by Anonymous Inner Class
b)Implementing the interface by Anonymous Inner
Class

a)Extending the class by Anonymous Inner Class

class One
{
void printOne()
{
System.out.println("One");
}
}
class AnonymousDemoByClass
{
public static void main(String Arg[])
{
One obj = new One(){
void printOne()
{
printTwo();
}
void printTwo()
{
System.out.println
("Two");
}
};
obj.printOne();
}
}



Output: Two







b) Implementing the interface by Anonymous Inner
Class

interface One
{
void printOne();
}
class AnonymousDemoByInterface
{
public static void main(String Arg[])
{
One obj = new One(){
public void printOne()
{
printTwo();
}
void printTwo()
{
System.out.println
("Two");
}
};
obj.printOne();
}
}

Output: Two

2)Parametrized Anonymous Inner Class:

Here we are implementing our Anonymous inner class as a
paramer to any method.

interface One
{
void printOne();
}

class ParameterizedAnonymousDemo
{
public static void main(String Arg[])
{
ParameterizedAnonymousDemo obj =
new ParameterizedAnonymousDemo();
obj.doSomething(new One(){
public void printOne
()
{

System.out.println("One");
}
});

}
public void doSomething(One objOne)
{
objOne.printOne();
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is the role of xml in core java?? and how we can use it?? can somebody give a sample program with explanation and from where i can read more about xml?????

1814


What is not object oriented programming?

517


What is the purpose of the enableevents() method in java programming?

597


What are different types of expressions?

558


What about method local inner classes or local inner classes in java?

563






When to use runnable interface vs thread class in java?

534


Can we execute a program without main() method?

539


Why we cannot override static method?

569


Difference between overriding and overloading in java?

589


How can we run a java program without making any object?

552


What is getclass () getname () in java?

683


What is the role of the java.rmi.naming class?

536


What is the difference between method and means?

576


What is boolean in java?

519


Why pointers are not used in java?

592