File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested class in an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Nested class in an interface" Watch "Nested class in an interface" New topic
Author

Nested class in an interface

Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
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

Joined: Oct 28, 2000
Posts: 16
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.


P.Nutan Prakash<br />SCJP2
Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
thanks that was a real good example
Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
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

Joined: Oct 23, 2000
Posts: 66
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

Joined: Oct 28, 2000
Posts: 16
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

Joined: Oct 28, 2000
Posts: 16
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

Joined: Oct 23, 2000
Posts: 66
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested class in an interface
 
Similar Threads
Nested Class
Inner classes in interfaces (how long has this been around?)
Inner class
Interface Question - Can you answer it?
Declaration and Access confusion...