The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


one more...

Posted by Christian Schmitt on December 13, 2000 at 9:01 AM

Hi, Kishori.

Thanks for such a good answer, now i understand why it did not work.

But i do not understand why the solution (in fact it's more an evasion than a solution) i found some days ago does work:

Mapped to the example we used, i did the following:

I put the method

protected B newB(String s, boolean b){ return new B(s,b);}

in class A and the method-call

B b1 = this.newB(s,b);

in the method f of class C (i will put the complete code of this classes to the bottom of this posting.)
Speeking in OO-terms, because an instance of C is an A, it has the method newB(...). But inside newB(...) the protected constructor of B is called to which this instance of C has no access, as you pointed out. Though it works. It seems to me as if 'this.newB(...)' is in fact 'super.newB(...)'?

Definitively: If i overwrite the method newB(...) in C with the same code as in A (so it should not change anything), the compiler complains.

I cannot decide whether this is a lack of understanding on my side or a not-so-lucky-design-feature of java, or if it even is wanted behaviour a i am not able to see why it is wanted.

Thank you,

Here comes the code:

--> Class A: <--

package apack;

public class A {

protected class B {
protected String s ;
protected boolean b ;

protected B (String s, boolean b) {
this.s = s ;
this.b = b ;
public void msg ( ) {
System.out.println ( "String = " + s + " Boolean = " + b );

//That's my 'evasion-method'
protected B newB(String s, boolean b){
return new B(s,b);


-->> and Class C: <<--

package cpack;

import apack.A;

public class C extends A {

public void f( String s, boolean b ) {
B b1 = this.newB( s, b );
b1.msg ( ) ;

public static void main ( String[] args ) {
new C ( ).f ( "hello", true ) ;


//Delete the '//' in the following lines to annoy the Compiler
//protected B newB(String s, boolean b){
// return new B(s,b);


(Besides: After i read your answer, i thought: Ok, i'll subclass A$B with a inner class D of C and create a new D (and with that a B) in f with
B b1 = new D(b,s);
but then the compiler complains far as i understand now, this complaining is about the call super() in the Constructor of D...)

  • U got it... Kishori Sharan December 13, 2000 at 2:24 PM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us