Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

inner class

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
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 {
}
</pre>
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.
 
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
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
<code>
B b = new A(10).new B();
</code>
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).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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
hey, maha anna i was puzzeled why c is not correct, it is
thanks for expln,
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic