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...

[1786 byte] By [fusion2005a] at [2007-11-26 19:54:07]
# 1

Smells like a bad design to me. A parent class should be more concise than a child class. That is a child class has everything a parent class has plus more. But if you insist on doing it this way then you can pass a dummy value but I think you should revisit your design.

class Child extends Parent {

Child(int a, int b) {

super (a, b, 10);

}

}

floundera at 2007-7-9 22:46:30 > top of Java-index,Java Essentials,New To Java...
# 2

> 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.

DrClapa at 2007-7-9 22:46:30 > top of Java-index,Java Essentials,New To Java...
# 3

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])

lethalwirea at 2007-7-9 22:46:30 > top of Java-index,Java Essentials,New To Java...
# 4
for class child only parrent2 is visible so u can use super(int , int, int , int , String )
baiju@suna at 2007-7-9 22:46:31 > top of Java-index,Java Essentials,New To Java...