Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class and misc Q 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 "Inner class and misc Q" Watch "Inner class and misc Q" New topic
Author

Inner class and misc Q

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

What snippet can be a part of a declaration of an inner class:
: (select all)
: A. private MyInner {
: B. new myInner() extends OtherClass {
: C. static class MyInner {
: D. abstract class MyInner {
A and B are not correct. C can't be inner class because it is a top-level class. D is right.
Can anyone check my answers please?
Which of the following statements is 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.
a ,b are not correct. You can't have an inner class inside an interface.
c is right. D is right too.
e is also right because it is a static inner class. Class declared with static keyword.
Can anyone check my answers please?. \
Thanks a lot
deepa
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Please correct me if i am wrong
=================================================================
The inner static class can be either public,private,protected,final or abstract.
The inner class defined in the method of the enclosing class cannot have the access modifiers public/private/protected
Member inner classes can be declared public,private,protected,final,abstract but cannot
have the same name as the enclosing class.
Coming to the first question take a look at the class definition below:
public class outernested
{
static int n=10;
public abstract static class innernested//Public,protected,private,final or abstract.
{
System.out.println(n);
}
}
This declaration is perfectly legal.
#2.Assume there is an interface t1
public interface t1
{
}
An interface can contain a nested top-level inner class--Is valid
public interface testInterface
{
public class A
{
public static class s
{
}

}
}
An interface can contain a member inner class--Is valid
public interface testInterface
{
public class A
{
B myB=new B();//member inner class
public class B
{
}
}
}

A member inner class can implement an interface--is valid
public interface testInterface
{
public class A
{
B myB=new B();
public class B implements t1
{
}
}
}

A static method can contain a local class.
public interface testInterface
{
public class A
{
B myB=new B();
public class B implements t1
{
}
}
static void method()
{
class C
{
}
}
}
}
The important point to be noted here is that the inner class defined in the method of the enclosing class cannot have the access modifiers public/private/protected
A static method can contain a nested top-level class. --Is wrong i guess going by the above rule.
public interface testInterface
{
public class A
{
B myB=new B();//member inner class
public class B implements t1//Interface
{
}
static void method()
{
static class C
{
}
}
public static class s
{
}
}
}
=================================================================
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Surya,
I deleted your repeated post in this thread. I think accidently you posted twice.
regds
maha anna
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
What snippet can be a part of a declaration of an inner class:
: (select all)
: A. private MyInner {
Deepa,
You are right to say A and B are wrong. However C is also right. An inner class declared with the Static modifier is refered to as a top-lebel inner class and is perfectly legal. However you can not have a top - level inner class declared inside a method. Thus you can not having something like
void someMethod(){
static class inner {//illegal
}
}
and the reason is that all variables declared inside method blocks ( or any other code block indeed) exist only while that block is existing. On the contrary, variables declared static static exist whenever there class is loaded. So you can not have a temporary-permanent variable.
This should help on question1 I guess.
Actually the above discussion should be enough to tell you that on question 2, e is WRONG. This is on the basis that as long as a class is within a method ( Regardless of whether the method is static or not), it can not be declared static.
A and B are definately wrong because inside interfaces you can only have method declarations,constant varaible declarations and subInterfaces, not classes.
I stand to be corrected.
Herbert
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
A. private MyInner {
...is absolutely a correct answer! This is used all of the time for (non-anonymous) inner listener classes. My last project had a ton of these!
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Java Nut,
A. private Inner {..
is not correct for the reason that it does not include the keyword class. It ought to have been ....
private class Inner{....
Herbert.
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi
Interfaces can only have method declarations,constant varaible declarations and subInterfaces, not classes.
If the above statement holds good then how is it possible to define an interface with class in it as well as execute the program.I know that no one defines classes with in an interface but there is no problem with the compilation of the program:
public interface abc
{
void method();
int a=1;
public class def
{
public static void main(String[] args)
{
System.out.println("I am in an interface");
}
}
}

and it generates two classes
abc.class
abc$def.class
and i can execute the main program of the class by typing in
java abc$def which prints out "I am in an interface".
So as you can have inner classes in a class,i am really confused as to can we have classes within an interface?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class and misc Q
 
Similar Threads
doubt - innerclasses & interface
inner class
innerclass question
Inner class concept
Nested class/interface doubt