• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inner class and misc Q

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

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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
{
}
}
}
=================================================================
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surya,
I deleted your repeated post in this thread. I think accidently you posted twice.
regds
maha anna
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic