I want to do following thing:
class P {
P(int a) {
// construct
}
}
class C extends P {
}
// in main
int a = 2;
C foo = new C(a); // can I do this?
I want create child object C by calling parent class P's constructor without writing any constructor in class C like "super(a)". Is that possible?
The idea is that I have a lot of class like "class C" which needs the same constructor functionality as "class P". So I don't want write a constructor method each time I create a new similar class.
Thanks
So,in your case Class C
has a default constructor which would try to implicitly call the default constructor of Class P
which doesn't exits and would fail.
So,you have to do it this way
class P
{
public P(int a)
{
// construct
}
}
class C extends P
{
public C(int x)
{
super(x);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With