What is the Use of Inner classes in Interface.
Hi All,
Most of us we know that We can define inner classes in the interface. Like
[b]publicinterface MyItf{
Demo d =new Demo();
class Demo{
Demo(){
}
//some additional code here
}
}[/b]
Now I have following question in my mind:
1. An Interface is pure abstract. Then why inner classes inside the interface?
2. In what scenario, we can utilize these inner classes of interface?
Plz Share your views on this...
Thks for ur replies in advance.
> 1. An Interface is pure abstract. Then why inner
> classes inside the interface?
Because one of the methods in the interface might return e.g. Demo, or takes Demo as an argument, but you don't want to make Demo a top level class if it's only used in relation to the MyItf interface.
> 2. In what scenario, we can utilize these inner
> classes of interface?
See above.
U said:
Because one of the methods in the interface might return e.g. Demo, or takes Demo as an argument, but you don't want to make Demo a top level class if it's only used in relation to the MyItf interface.
This we cando in defining Demo Class outside. Ok Also tell me how to pass an Object in inner class Demo. to the method of Interface.
Can u give some real time situation where this concept can be used.
Hoping for ur reply, Thks for this one.
> This we cando in defining Demo Class outside.
That's no argument. You could write the programs in other languages, so why use Java? Just because you can use a top-level class instead, it's no argument against using an inner class. You also can make all attributes public... you don't o that either (I hope).
> Ok Also
> tell me how to pass an Object in inner class Demo. to
> the method of Interface.
public abstract TheInterface.Demo doSomething(TheInterface.Demo d);
> Can u give some real time situation where this
> concept can be used.
There are only very, very few. Just because it's possible, it doesn't mean it needs to be done or is done often.
public interface MyItf{
Demo d = new Demo();
class Demo{
Demo(){
System.out.println("Hello World!");
}
void meth(){
System.out.println("from meth");
}
//some additional code here
}
}
class zz
{
public static void main(String[] args)
{
MyItf m= new MyItf();
m.meth();
System.out.println("Hello World!");
}
}
i hav write a class like above, Now i want to runt tht meth()
how cani do tht....
plz let mw know
You can't, because you can't construct an interface, and because m.meth() doesn't exist.Possibly you meant MyItf.d.meth(). Almost impossible to say with all the typing errors. Please try harder.
ejpa at 2007-7-10 3:03:44 >
