Author Topic:   is no-arg ctor provided auto for subclass?
maha anna
bartender
posted February 09, 2000 12:10 AM             
I thought when the compiler sees class C extends B ,the foll code will not compile because of the absence of B()
But it does compile without error. Can sombody expalin whether B() is provided automatically.

code:

class A {
public A() {
}
}

class B extends A {
public boolean B(String msg) {
return false;
}
}

class C extends B {
private C() {
}
public C(String msg) {
}
public static void main(String[] args) {
C c = new C();
}
}


[changed CODE to code, and added /code as well at end - Jim]

[This message has been edited by Jim Yingst (edited February 09, 2000).]

Thandapani Saravanan
bartender
posted February 09, 2000 01:08 AM             
When the compiler sees a class (without a super class) without a constructor, it just adds the default no-argument constructor. If the class is inherited from another, then it adds default no-argument constructor with super() in the body.

So, here inside class B the following constructor is added:

public B() {
super();
}

maha anna
bartender
posted February 09, 2000 06:37 AM             
Thank you Mr. Saravanan.

Lucy C
ranch hand
posted February 09, 2000 08:06 AM             
However, if the confusingly named method B (very sneaky!) in the B class was a constructor that took a String parameter rather than a method, the whole thing wouldn't compile. Java only provides a default no-arg constructor if there aren't any other explicit constructors....

Tony Alicea
sheriff
posted February 09, 2000 12:00 PM             
Good catch, Lucy C!

maha anna
bartender
posted February 09, 2000 12:30 PM             
Thank you Lucy. The whole confusion happened because I WAS NOT READING PROPERLY. I thought the class B had a Constructor taking a String arg without seeing the boolean return type properly.
Then I realized that my doubt was correct. I think I shd practice to read the question CAREFULLY.

Thank you all

|