Constructors and super(...)
Hey...what if the parent class has more parameters in constructor and child class has less.. how do u call a super then...?
for e.g.
publicclass Parent1{
public Parent1 (int a,int b,int c, String d){
......
}
}
publicclass Parent2extends Parent1{
public Parent2 (int a,int b,int c,int d, String e){
super (a, b, c, e);
}
}
publicclass Childextends Parent2{
public Child (int a,int b,int c, String d){
super(a,b,c,d);
}
}
Child is extendingParent2 which extends Parent1.
This gives me constructor problems.. Do i have to do super super or what...
Thanks again guys...
> Smells like a bad design to me.
Smells like somebody just making up random stuff. If you had real classes that were supposed to do real things then you could ask a real question about how those constructors should work. But since they are just meaningless lumps of code, the question about how the constructors should work is also meaningless.
When you are calling super() in your child class, you are calling the super class of child, which is ONLY parent 2.
It goes directly to Parent2's constructor, which ONLY takes 5 arguments, 4 ints and a String data type.
So in the child class, you would have to call
super(int arg1, int arg2, int arg3, int arg4, String arg5)
Otherwise, You'll just get an error because in your constructor of the Child class, you are telling the compiler to look for a method in Parent2 class that takes 3 ints and a String(Which doesn't exist)
You can fix this problem by editing the Parent2 class, making it a constructor that takes 3 int values and a String argument. (You can do this without having to delete the old parent 2 method too [overloading])