The problem here is that what you are trying to achieve has little to do with inheritance.
Actually, let's say you have this:
class A { int a; public A(int a){ this.a = a; } public int get(){ return a; } public String whoAmI(){ return "I am class A"; } } class B extends A { int b; public B(int a, int b){ super(a); this.b = b; } public int getA(){ return super.get(); } public int getB(){ return b; } public String whoAmI(){ return "I am class B"; } }
So now you can do what soars:
A a = new A(2);
or
B b = new B(3,2); // Create a new instance of B
Note that when creating a new instance of B, you must also pass a parameter to create the super A class, as if you were copying both of them at the same time. This is the meaning of inheritance, by building B, you are actually building a new element A plus some advanced features that are specific to B.
What you want to do is something more:
A a = new A(2); B b = new B(3,a);
Let's see what this class B looks like:
class B { A a; int b; public B(int b, A a){ this.b = b; this.a = a; } public int getA(){ return a.get(); } public int getB(){ return b; } public String whoAmI(){ return "I am class B"; } }
Here you see that you can get something like what you want, but in this case B is not a child of A, it is more like a wrapper, and A is just an attribute of B. You can also add a method to B, for example:
public A a(){ return a; }
And then just call:
baget();
See, this has nothing to do with inheritance, as the latter relates more to "When I create a child class, I actually also create the parent class alltogether and thus can use any child class as if it were a parent." ..
EDIT
Ok, I figured out an example to show in code what @David W said in his answer:
class Tool { int age; int value; public Tool(int age, int value){ this.age = age; this.value = value; } public void use(){ this.age++; this.value--; } } class ScrewDriver extends Tool{ boolean starTip; int tipJamming = 0; public ScrewDriver(int age, int value, boolean starTip){ super(age,value); this.starTip = starTip; } public void screw(){ super.use(); this.tipJamming++; } } class Hammer extends Tool{ int weight; public Hammer(int age, int value, int weight){ super(age,value); this.weight = weight; } public void hammer(){ super.use(); } }
So, you have a parent Tool class and two children ScrewDriver and Hammer. If what you are saying, perhaps you can write this:
Tool screwDriver = new screwDriver(1,10,true);
This means that you actually use a screwdriver as a hammer, but that actually violates the whole idea of ​​inheritance, because if you can use any tool for each other, then what is the need to define their different tools? you can simply implement all the methods that you need in the Tool class, and use them if necessary depending on the context, for example:
class Tool{ ... attributes and constructor ... pulic void use(){ this.age++; this.value--; } public void screw(){ use(); this.tipJamming++; } public void hammer(){ use(); } }
and then
Tool screwDriver = new Tool(...); Tool hammer = new Tool(...); screwDriver.screw(); hammer.hammer();