• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

innerclass question

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone help me with this.Which of the following statements is true? I think a,b,c,d are true.
a) An interface can contain a nested top-level inner class.
b) An interface can contain a member inner class.
c) A member inner class can implement an interface.
d) A static method can contain a local class.
e) A static method can contain a nested top-level class.
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me with this.Which of the following statements is true? I think a,b,c,d are true.
a) An interface can contain a nested top-level inner class.
b) An interface can contain a member inner class.
c) A member inner class can implement an interface.
d) A static method can contain a local class.
e) A static method can contain a nested top-level class.
I don't know , but i think that interface can only contain a static class within it. C is true i think and D is true too don't know about A
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So A must be right and B is not?Can anyone please explain...
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mindy Hudson:

Can anyone help me with this.Which of the following statements is true? I think a,b,c,d are true.
a) An interface can contain a nested top-level inner class.
b) An interface can contain a member inner class.
c) A member inner class can implement an interface.
d) A static method can contain a local class.
e) A static method can contain a nested top-level class.



Option A is right as Normally you can�t put any code inside an interface, but a static inner class can be part of an interface. Since the class is static it doesn�t violate the rules for interfaces � the static inner class is only placed inside the namespace of the interface:
interface IInterface {
static class Inner {
int i, j, k;
public Inner() {}
void f() {}
}
}
So Option B is wrong..
Option C is right as any class can implement the interface...
Option D is right as the class that is inside static method will become static local class......So that it can only access the static memnbers of enclosing class n object of local class can be created with out need of object of enclosing class.

Option E is wrong as nested top level classes cant be inside methods..

 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Viral.You were of great help.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe A, B, C, and D are correct. E is not correct.
Below is an example that runs for B.
public class test implements Timer1 {
public static void main(String args[]) {
test t1 = new test();
Timer1 timerVar = t1;
nested testNested = timerVar.new nested();
testNested.testMethod();
}
public void timerFired() {}
}
public interface Timer1
{
public abstract void timerFired();

class nested {
public void testMethod() {
System.out.println("test method"); }
}
}
Please explain any difference if you feel that b is correct.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
A. True. All member classes in an interface are implicitly
static (JLS §9.5); a top-level nested class is a
static inner class
B. False. As all member classes are implicitly static (see A)
an interface cannot contain a non-static inner class
C. True. A member class can implement another
interface.

D. True. ( see code below E )
E. False. Local classes can have no access modifier or be
declared final.

Hope that helps.

------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one thaks for the answers.Will be posting more doubts.
 
reply
    Bookmark Topic Watch Topic
  • New Topic