How to use overloading and polymorphism effectively
I have BaseObj
:
public abstract class BaseObj {
String name;
public BaseObj(String _name) {
name = _name;
}
public void report(){
System.out.println(name + " is " + getType());
}
public abstract String getType();
}
and two subclasses Sample1
and Sample2
:
public class Sample1 extends BaseObj{
private float var;
public Sample1(String name,float _var){
super(name);
var = _var;
}
@Override
public String getType() {
return "Float: " + Float.toString(var);
}
}
and
public class Sample2 extends BaseObj{
private int var;
public Sample2(String name , int _var){
super(name);
var = _var;
}
@Override
public String getType() {
return "Integer: " + Integer.toString(var);
}
}
in main calss:
public class Poly {
public static void main(String[] args){
BaseObj mObj[] = new BaseObj[4];
// Hard-definition of the object tyte
mObj[0] = new Sample1("X1",(float)12.34);
mObj[1] = new Sample2("X2",12);
mObj[2] = new Sample2("X3",12);
mObj[3] = new Sample1("X4",(float)1.2);
for(BaseObj x:mObj){
x.report();
}
}
}
I need to hardcode the type of the mObj elements. But I'm looking for a way to use overload
to avoid this kind of tricky definition for example. in the new class, I am using overloading to get the correct object based on its inputs:
public class Sample{
public Sample(String _name , int _var){
// get Sample2 object
}
public Sample(String _name , float _var){
// get Sample1 object
}
}
then I could change my main calss code like this:
public class Poly {
public static void main(String[] args){
BaseObj mObj[] = new BaseObj[4];
mObj[0] = new Sample("X1",(float)12.34);
mObj[1] = new Sample("X2",12);
mObj[2] = new Sample("X3",12);
mObj[3] = new Sample("X4",(float)1.2);
for(BaseObj x:mObj){
x.report();
}
}
}
the output currently looks like this:
X1 is Float: 12.34
X2 is Integer: 12
X3 is Integer: 12
X4 is Float: 1.2
Edit: What I need is to define the mObj elements as new Sample("X1",var)
, not some as new Sample1("X1",(float)12.34)
and some as new Sample2("X1",12)
. For this, I decide on the type of my object in the class constructors Sample
. Anyone have an idea? Thanks to
source to share
I think you are looking for a factory method here.
public class BaseObjFactory {
public static BaseObj create(String name, int value) {
return new Sample2(name, value);
}
public static BaseObj create(String name, float value) {
return new Sample1(name, value);
}
}
and use it this way
mObj[0] = BaseObjFactory.create("X1",12.34f);
mObj[1] = BaseObjFactory.create("X2",12);
mObj[2] = BaseObjFactory.create("X3",12);
mObj[3] = BaseObjFactory.create("X4",1.2f);
Btw. no need to use a listing (float)1.2
, just add f
to make it a literal float
.
source to share
Use factory methods.
class SampleFactory {
Sample1 create(String name, int value) {
return new Sample1(name, value);
}
Sample2 create(String name, float value) {
return new Sample2(name, value);
}
}
Then you can use it like
mObj[0] = SampleFactory.create("X1",(float)12.34);
mObj[1] = SampleFactory.create("X2",12);
mObj[2] = SampleFactory.create("X3",12);
mObj[3] = SampleFactory.create("X4",(float)1.2);
source to share
I suggest you start with the simplest solution for what you are trying to achieve.
public class Poly {
public static void main(String[] args) {
Sample[] mObj = {
new Sample("X1", 12.34f),
new Sample("X2", 12),
new Sample("X3", 12),
new Sample("X4", 1.2f)
};
for (Sample x : mObj)
x.report();
}
}
class Sample {
private final String desc;
private final Number value;
public Sample(String desc, Number value) {
this.desc = desc;
this.value = value;
}
public void report() {
System.out.println(desc + " is a " + value.getClass().getSimpleName() + ": " + value);
}
}
prints
X1 is a Float: 12.34
X2 is a Integer: 12
X3 is a Integer: 12
X4 is a Float: 1.2
This takes advantage of polymorphism in that Float and Integer are both subclasses of Number.
source to share