File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested class in an interface

 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the jls (first page of chp 8) it says :
" A nested class is any class whose declaration occurs within the body of another class or interface"
Is this saying that I can create an inner class inside of an interface like this:
interface aninterface {
public class inner {
// do something in some method
}
}
Why am I allowed to do this?
And what possible purpose could this serve, I mean how
would a class that implements this interface use this inner class.
 
nutan prakash
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Below is a very 'vague' example. But, having an inner class inside an interface does help. Like when you want come common implementation to be shared across objects of TYPE 'abc'.
see the code....
import java.util.*;
import java.awt.*;
import java.awt.event.*;
class A
{
A(){System.out.println("A's Constructor called");}
void disp(){System.out.println("A's DISP Called");}
}
interface abc
{
public class innerabc
{
void disp(){System.out.println("inner abc");};
}
}
class AA extends A implements abc
{
AA(){System.out.println("AA's Constructor called");}
void disp(){super.disp();System.out.println("AA's DISP Called");}
}
class test
{
private static void main(String args[])
{
abc aa=new AA();
abc.innerabc ac=aa.new innerabc();
// USE THE INNER CLASS HERE.
//aa.disp();
}
}
bye.
 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks that was a real good example
 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have the main method as private?
I thought it had to be public.
When is it ok for it to be private like this?
 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I took out the comments for aa.disb() it gave me a'
"can't resolve symbor error:.
This would make since to me if it gave me this error at
super.disp() that inside the disp() of AA because it won't know whether to use the abc disp or the A disb().
But its pointing at the aa.disp() when its giving me an error.
At that point it should know which disp() I want. I want the one in AA since aa is an AA .
You knew this would happen thats why you commented it out,
but can you tell what is the problem with having aa.disb() there.
 
nutan prakash
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can have the main method as Private. It will do no harm.
Regarding the other one.... that method is part of the inner class of abc. You can't call it directly.
Pardon me.... I was experimenting a bit with inner classes, and that was when I saw your message and thus posted a reply... some part of the code, is not related to what you had asked...
bye...
 
nutan prakash
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just another thing....
when you say something like....
interface_object a=new impl_class();
Where
interface_object - is the interface AND
impl_class - is the class implementing the above interface
Then you can call the method present ONLY in the interface! Similar to what u do in RMI.
bye.
 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right I think having mian as public is only a convention
but when you go for the exam remember that despite the fact that practicaly you can have a protected private or public or even friendly modifier for main it the real exam if they give you
choices of valid main statements, don't pick any of them that are not public. You'll get the question wrong. So as far as the test is concerned it has to be public.
You're right about the second thing also. I just wasn't lookin closely enought.
Thanks again
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic