Reputation: 13
I have another question on constructors. I have a hierarchy of more than 2 layers, Say
A
, B extends A
, C extends B
. Can I get constructors from all three layers to fire when I create an instance of C? What if I want to pass super(int anArgumentForA, int anArgumentForB);
Is that even possible? Seems like that's where it's going but I don't find a way to differentiate the arguments going to A
vs B
. Default constructor from B
seems to run and I can't feed it the value it needs in order to run a different constructor because I don't know how.
Upvotes: 1
Views: 118
Reputation: 6525
As per Java Specification the constructors are not inherited by sub-classes. So, as per you requirement you should keep in mind that the super class should have scope to pass objects.
When you are declaring a class by extending another class B extends A
and C extends B
, you should define the constructor
such a way so that you can customize that for initialization . This declaration always depends on the depth and hierarchy of the
inheritance .
class A{
A(Object firstObj, Object secondObj){
//Do you stuff for initialization
}
}
class B extends A{
//Keep in mind that this B class will be inherited by other class
B(Object firstObj,Object secondObj,Object thirdObj){
//Call super class constructor
super(firstObj,secondObj);
//Do other initialization stuff here
}
}
class C extends B{
//Keep in mind that this C class will be inherited by other class
C(Object firstObj,Object secondObj,Object thirdObj){
//Call super class constructor
super(firstObj, secondObj, thirdObj); //call constructor of B
//Do other initialization stuff here
}
}
It always depends how you parent class constructor has defined. It depends on the number of parameter passes to parent class constructor, you need to call accordingly from child class constructor.
Hope it will help you.
Upvotes: 0
Reputation: 40066
In the world of Java, when you are extending another class, you only see your direct super class' constructor. The super class is supposed to proper encapsulate the super-super class constructor.
That means, in your case, B should provide a constructor that proper encapsulate A. In case B cannot determine the argument to pass to A's constructor, it is reasonable to ask for it in B's own constructor:
class A {
A (int aArg) {
// init base on aArg
}
}
class B extends A {
B (int aArg, int bArg) {
super(aArg);
// extra initialization base on bArg and aArg
}
}
class C extends B {
C() {
super(SOME_A_ARG, SOME_B_ARG);
}
}
Upvotes: 3
Reputation: 2534
You can have each layer call super(...) to set the information in each layer.
public Class A
{
public A(Object a, Object b)
{
//do any initialization needed
}
}
public Class B extends A
{
public B(Object a, Object b, Object c)
{
super(a, b); //This will call A(a, b)
//do any initialization needed
}
}
public Class C extends B
{
public C(Object a, Object b, Object c)
{
super(a, b, c); //This will call B(a, b, c)
//do any initialization needed
}
}
Keep in mind I don't think it is advisable to use these constructors to set the Objects to a variable at each level. So you don't want to have the constructors for A
, B
, and C
each set some object Object x = a
in the constructor. Only one, at most, level in the hierarchy should maintain a reference to the Objects being passed into the constructors.
Upvotes: 3