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 class defined inside an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class defined inside an interface " Watch "class defined inside an interface " New topic
Author

class defined inside an interface

rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Which of the following are true about the class defined inside an interface
1. it is not possible in the java Laungage.
2. The class is always public.
3. The class is always static.
4. the class methods cannot call the methods declared in the interface.
5. the class methods can call only the static methods declared in the interface.
Answer given: 23 (Abhilash Q60)
I think item 4. is also true. As the class is declared in a static context, it can not call methods declared in the interface as they are non-static.
Any comments?
Aru
Ranch Hand

Joined: Jul 20, 2000
Posts: 112
Hi all,
Can anyone give an example how to use the class which is declared in a interface in a class which implements the interface.

Help is greatly appreciated.
Aruna



Sarang Khankhojr
Greenhorn

Joined: Jan 31, 2005
Posts: 17
Hi guys,
I thought interfaces do not have instance variables it can have
only abstract methods and constants(public static final).
So how can there be a class inside an interface?
Please explain.
Sarang
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
Hi rajsim,
Yes I think you are right. A class defined inside an interface is always public static by default. Methods defined inside an interface are public abstract by default (they cannot be static). Hence the static class cannot make a non-static reference to the interface methods. So 4 should be true.
We have had a similar discussion on this in Marcus Green's forum: http://www.jchq.net/discus/messages/1/836.html?MondayJuly1720000744am
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
OK, this is much more involved than is necessary to understand for the exam. But since you asked:
It's true that a class defined inside an interface is implicitly static, which means that it's a static member class (a so-called "static inner" class). However, this does not mean that it cannot make reference to non-static methods. It's true that you can't do this from a static method or from a static context, but (perhaps surprisingly) a static class does not have this limitation. Observe:
<code><pre>
interface MyInterface {

void method1();
void method2();

// implicitly static class:
abstract class Abstract implements MyInterface {
public void method2() {
System.out.println("In method2()");
method1(); // See?
}
}
}

public class Test {
public static void main(String[] s) {
MyInterface mi = new MyInterface.Abstract() {
public void method1() {
System.out.println("In method1()");
}
};
mi.method2();
}
}
</pre></code>
The above code compiles and runs, printing out
<code><pre>
In method2()
In method1()
</pre></code>
The key point here is that the static member class Abstract contains a call to non-static method1(), declared (but not yet implemented) in the containing interface. Note that in order to do this, the class had to be declared to implement the containing interface. This meant I had to either (a) implement method1() and method2() in the class, or (b) declare the class abstract. I chose (b) because this way it's clear that method1() refers to the method1() declared in the interface, not to some overriding declaration. For comparision, here's option (a):
<code><pre>
interface MyInterface {

void method1();
void method2();

// implicitly static class:
class Implementor implements MyInterface {
public void method1() {
System.out.println("In method1()");
}
public void method2() {
System.out.println("In method2()");
method1(); // See?
}
}
}

public class Test {
public static void main(String[] s) {
MyInterface mi = new MyInterface.Implementor();
mi.method2();
}
}
</pre></code>
Here again, method2() contains an invocation of the method1() declared in the interface, but this isn't quite so impressive since the implementation of method1() is right there in the same static member class.
So back to the original question - answer 4 is incorrect. I can't imagine why you would ever want to do this though, and I'm sure that this detail won't be on the exam. So if the preceding explanation seems confusing, don't worry about it too much.

"I'm not back." - Bill Harding, Twister
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Thanks Jim for your explanation.
Inner classes can call methods in the enclosing interface
if they implement that interface and the ability to call
these methods has nothing to do with inner-outer relationship
but is because of inheritance.
However the statement 4. will be false due to the above
condition
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class defined inside an interface
 
Similar Threads
class defined inside an interface
doubt in interface??
interface
Interface??
class inside interface