Author Topic:   nested class in interface
Mani
unregistered
posted April 20, 2000 09:40 AM           
A question from IBM mock exam

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

What they really mean by nested top level onner class inside an interface??
does it mean

interface face
{
class temp
{

}
}
(if this is what they meant then it is valid ,i think)
or

interface face
{
class test
{
class test
{

}
}
}


I implemented these condns in a program a,b,c,d are compiling
e is not compiling.

maha anna
bartender
posted April 23, 2000 07:31 PM             
Mani ,
Nested top level class means a static inner class . I am giving you an example program and Maha's comment also for you to play with. Have fun .
regds
maha anna


interface inter {
//int i; //error final var MUST be initiated

public final int i1 = 100; //ok

void m1(); //ok

public abstract void m2(); //ok only these 2 forms are ok for methods

//public final void m3(); //no native/final/synchronized/private/priotected

static interface in1 {} //ok nested interface

interface in2 {} //ok. Does it mean implicit static ? I think so
//because see below. interface inter1 can extend inter.in2 ok

static class c11 {} ///ok nested top-level class ok

class c22 {} //ok

//{ } //not ok . NO static init in interfaces !!!! Note that WORD STATIC.
//implies it is implicit static

//static {} //not ok as above

}

interface inter1 extends inter.in2 {} //ok


abstract class c1 implements inter {
//public final int i1 = 100; // ok by default all var come from an interface are
//implictly public static final and has a val. But in the class you can
//choose to refint the SAME var as you wish as instance var or whatever

//class chk extends c1.c11 {} //not ok

class chk extends inter.c11 {} // ok. Does it mean only VAR & METHODS are
//inherrited from the interface ? I think so.
//class chk1 extends c22 {} //not ok super class c22 of inner class c1.chk1
//not found err

class chk2 extends inter.c22 {} // ok. this measn the inner class in inter

//is implicit static
public static void main(String[] ate) {
System.out.println(c1.i1);
}
}


[This message has been edited by maha anna (edited April 23, 2000).]

Mani
ranch hand
posted April 23, 2000 11:48 PM             
This code was really helpful.
But one more thing. Why
public final int i1 = 100; in class c1 doesn't produce any error.
We are implementing inter .(as in your next line you have proved that only methods and variables of inner class are inherited,so I think we are inheriting final var i1 also.). In c1 we are trying to change the value of final static variable inherited from inter.Shouldn't it give an error??(Although it is not giving any)

maha anna
bartender
posted April 24, 2000 01:23 PM             
Mani,
Your doubt is correct. You have to understand one more point in inheritance of var. In case of var inheritance, you can re-declare the inheritad var as whatever you want. This is not like method inheritance. In method inheritance only, we have to be careful about whether we are overriding or hiding accordingly we should re-define the method in subclass.

But in case of var, the subclass hides the inherited ones. JLS says this is hiding of inherited variables. JLS relaxes the ruls for var hiding . In case of method hiding we have to be careful while hiding a static method. We can't hide a static method to be non-static. But in case of var hiding we can.

Refer to JLS here Classes /4th paragraph/last sentence


From JLS
--------
The members of a class include both declared and inherited members (§8.2). Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface

Try this example. This code will compile and run without any problem.


class Base {

public static final int BASE_INT = 100;
public final int BASE_INSTANCE_INT = 100;
public final int BASE_FINAL_INT = 100;
}

interface inter {
public static final int INTER_INT = 100;
}

class test extends Base implements inter{

//make inherited (from base class) static var as instance var ....OK
public final int BASE_INT = 100;

//make inherited (from interface) static var as instance var ....OK
public final int INTER_INT = 200;

//make inherited (from base class) instance var as static var ....OK
public static final int BASE_INSTANCE_INT = 300;

//make inherited final (from base class) instance var as non-final var ....OK
public int BASE_FINAL_INT = 400;


void print() {
System.out.println("Base_int = "+BASE_INT);
System.out.println("Inter_int = "+INTER_INT);
System.out.println("Base_instance_int = "+BASE_INSTANCE_INT);
System.out.println("Base_final_int = "+BASE_FINAL_INT);
}

public static void main(String[] args) {
new test().print();
}
}


[This message has been edited by maha anna (edited April 24, 2000).]

Eric Barnhill
ranch hand
posted April 25, 2000 09:42 PM         
My question is, what good is a final var in an interface then? I can see with a class, even if a subclass could change it, then it would have value within the base class. But since one always has to implement an interface, and you can change the var, final seems to me of no power .

Eric B.

|