Author Topic:   inner class
Umesh
ranch hand
posted April 02, 2000 09:51 AM             

class A {
protected int i;
A(int i) {
this.i = i;
}
}
Which of the following would be a valid inner class for this class?

Select all valid answers.

a)
class B {
}
b)
class B extends A {
}

c)
class B {
B() {
System.out.println("i = " + i);
}
}

d)
class B {
class A {
}
}

e)
class A {
}

Ans: a
Question: why not 'c' also ?.
My thinkin...Class B is not extended from Class A and hence B() will not refer to Class A.

Howard Stern
ranch hand
posted April 02, 2000 10:25 AM             
This question has already been discussed previously.
C is indeed a valid answer. However not because B does not extend A. An inner class should not extend the outer class. It is treated as if it is another member of the class A. Only you must remember that you cannot instantiate an inner class without associating it with a reference of the outer class. An inner class is useful when you want to create an object in the context of another object.
i.e

B b = new A(10).new B();

In this example the member variable int i of the outer class A shall be assigned the value 10 and the constructor of the inner class shall print "i=10". The member variable of the outer class is accessible from the inner class

[This message has been edited by Howard Stern (edited April 02, 2000).]

maha anna
bartender
posted April 02, 2000 04:27 PM             
However not because B does not extend A. An inner class should not extend the outer class. It is treated as if it is another member of the class A.

Howard, I caught you here. An inner class CAN EXTEND outer class / any class as long as it has the access level.
Answer b) is wrong NOT because it extends the outer class. It is indeed a valid inner class. But the reason why it can't be added to the outer class without a compiler error is outer class A does not have it's default no-arg constructor. The compiler does not provide one because the user had defined another taking a int arg. This is the reason. If the outer class WOULD HAVE defiend A() {} then b) is also correct ans.
So,
a) correct
b) Not correct // Since A() {} does't exist in outer class A
c) correct // as explained above
d) not correct // an inner class can't have the SAME name as any of its enclosing classes
e) not correct // same reason as d)
regds
maha anna

Shiva
ranch hand
posted April 03, 2000 11:15 AM         
hey, maha anna i was puzzeled why c is not correct, it is
thanks for expln,

go_mango
greenhorn
posted April 04, 2000 06:35 AM             
Another way to think of constructor chaining: I use a model of the grounding of electric circuits. But ofcoarse, it is a grounding of constructor calls.
Appears that constructor chaining between superclass and subclass is quite a bit like overriding, that is; the constructor signatures must match. I think of this chaining like grounding.
IF so AND an explicit constructor is written
AND an implicit no-arg constructor is not provided (as is the case)
THEN, a subclass can only chain to its superclass constructor thru the constructor signatures provided. In the case above, the no-arg constructor is not grounded, that is not connected to a superclass constructor.

[This message has been edited by go_mango (edited April 04, 2000).]

|