I think I already know the answer, but want to double check. If I
have two classes defined:
public class A {
public A() {
...
}
public A(int i) {
...
}
}
public class B extends A {
public B() {
...
}
}
Is there any way to prevent a programmer from calling
B b = new B(10);
other than documentation saying please don't? Are all methods
inherited by B from A always accessible to the programmer?
-Will
> I think I already know the answer, but want to double check. If I
> have two classes defined:
[quoted text clipped - 21 lines]
>
> -Will
Constructors aren't actually methods, and B doesn't inherit A(int i)
On the other hand, B() can chain to either A() or A(int i):
public B() {
super(10); // like "new A(10)"
}
public B(){
super(); /* Not technically needed. explicitly called if no other
super() is provided */
}

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Are Nybakk - 08 Nov 2007 19:57 GMT
>> I think I already know the answer, but want to double check. If I
>> have two classes defined:
[quoted text clipped - 34 lines]
> super() is provided */
> }
To clarify, "super" is then a reference to the class' superclass.
Another example:
public B(type param1, type param2) {
//call superclass' constructor to initialize inherited properties
super(param1);
//do initialization of B class properties
ownParam = param2;
}
Daniel Pitts - 08 Nov 2007 20:02 GMT
>> public B() {
>> super(10); // like "new A(10)"
[quoted text clipped - 18 lines]
>
> }
Actually, in this case super is a keyword that tells the compiler that
you wish to invoke a specific version of the constructor in the parent
class. Similar to the this() constructor chaining.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
> I think I already know the answer, but want to double check. If I
> have two classes defined:
[quoted text clipped - 19 lines]
> other than documentation saying please don't? Are all methods
> inherited by B from A always accessible to the programmer?
Yes, it is prevented as constructors are not inherited. So, if you don't
define a constructor in B that takes an int, then new B(10) is not possible.
//Roger Lindsjö
larkmore@aol.com - 08 Nov 2007 19:45 GMT
> Yes, it is prevented as constructors are not inherited. So, if you don't
> define a constructor in B that takes an int, then new B(10) is not possible.
Well bust my buttons! I always thought that constructors were
inherited. Works out for me well for me that they aren't. I guess
you learn something new everyday. Thanks guys. :)
-Will