Reputation: 97
I have two subclasses from a superclass
public class A {
}
public class B extends A{
int weight;
}
public class C extends A{
int height;
}
Now, I need to have an array named myArray that some cells are from class B and the others are from class C.
A[10] myArray;
myArray[2]=new B();
myArray[3]=new C();
myArray[2].weight=40;
myArray[3].height=60;
How can it be possible? Am I going in the right way?
Upvotes: 0
Views: 1514
Reputation: 85
If I understand it correctly, Below is what you are trying to do 1) You have a parent class which will hold common entities. 2) Child classes will hold specific entities for that specific child 3) You want to access both the child and parent objects.
If above is correct then you can use the implementation you mentioned with few changes. I would do something like below: (Pseudo code)
Class A{
//Common entities and getter/setters
String firstName;
String lastName;
}
Class B extends A{
Float height;
//Getter/ setter
}
Class C extends A{
Float weight:
//Getter/setter
}
And you can create an object of class B or class C and can set or get value accordingly.
Hope this helps.
Upvotes: 0
Reputation: 48307
I will go like this: define an interface that you can use for set w or h in an abstract way...
interface IGiveContent {
void takeInt(int someInt);
}
abstract class A implements IGiveContent {
}
class B extends A {
private int weight;
@Override
public void takeInt(int someInt) {
weight = someInt;
}
}
class C extends A {
private int height;
@Override
public void takeInt(int someInt) {
height = someInt;
}
}
public class Test {
public static void main(String[] args) {
IGiveContent[] foo = new IGiveContent[4];
foo[0] = new B();
foo[1] = new C();
foo[2] = new B();
foo[3] = new C();
//
foo[0].takeInt(111);
foo[1].takeInt(222222);
foo[2].takeInt(33332);
}
}
Upvotes: 0
Reputation: 22625
The thing you described is duck typing. This is actually possible to achieve in Java using reflection.
myArray[2].getClass().getDeclaredField("weight").setInt(myArray[2], 40);
but this is not very good way to go.
You should rather just cast object and then access property:
((B)myArray[2]).weight = 40;
Groovy is language for JVM which uses duck typing by default, so you in Groovy you could be actually able to write:
myArray[2].weight = 40;
Upvotes: 0
Reputation: 43234
It is possible to put all objects that inherit from A
into the same array, but to use an object specific field, you either have to resolve to casting or set the field before putting into the array. I prefer the later
A myArray[10];
B b = new B();
C c = new C();
b.weight=40;
c.height=60;
myArray[2] = b;
myArray[3] = c;
You could easily generalize this method to work for lots of B
and C
objects. So instead of having just the variables b
and c
, you could have an array of them; then when you are done with setting their values you can put them all in the array of A
.
For the sake of completeness, here is the casting approach (Notice how much uglier)
A myArray[10];
myArray[2] = new B();
myArray[3] = new C();
((B)myArray[2]).weight=40;
((C)myArray[3]).height=60;
Also note that if you cast it to the wrong type at runtime, you get the nasty ClassCastException
exception
Upvotes: 1