• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class defined inside an interface

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic