Hi all,

I have an object, say its called MySuperType, which has a sub type, say it is called MySubType.

MySuperType has the following attributes:

int a;
int b;
int c;


MySubType has the following attributes: (besides inhereting a,b and c)

int d;
int e;


I was wondering if I can do something like the following:

MySuperType mytype = null;
if (sub_type) {
     mytype = new MySubType ();
     mytype.setD(4);
     mytype.setE(5);
} else {
     mytype = new MySuperType ();
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);

sub_type is a boolean variable which will tell me which object type to create.

Can someone please comment.


Thanks,

I think:

MySubType mytype = null;
//instead of MySuperType mytype = null;

if (sub_type) {
     mytype = new MySubType ();
     mytype.setD(4);    
     mytype.setE(5);
} else {
     mytype = new MySuperType ();
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);

MySuperType mytype = new MySubType ();
No because superType is not a subType. Super type does not have d, e variables.
Sub: has a,b,c,d,e so it does not "fit" in the Super which has a,b,c

MySubType mytype = new MySuperType ();
Yes because SubType is a SuperType. Sub type has a,b,c
MySuperType has a,b,c and it fits into the larger Sub

Try to compile and run both with sub_type true or false and see which one is correct.

Please correct me If I am mistaken because I don't have time to test it

I am not sure, if that will work.

MySuperType mytype = null;
if (sub_type) {
     mytype = new MySubType (); //should be ok
     mytype.setD(4);
     mytype.setE(5);
} else {
     mytype = new MySuperType (); //Most likely a compiler error
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);

When you do something like--

Object obj = new String("Upcasted!");

--it is called upcasting. String is a derived form of an Object, so this code is valid. However the reverse shouldn't be true--

String str = new Object() // WRONG!

--Because Object can contain data of any derived type, like we have shown above. It may be possible that the data stored in the Object has nothing to do with the data in the String class. Now if you do this--

String str = (String)new Object(); //Not quite right

--you're downcasting the object from its current type to a type it might consist data of. Since it's a new Object it is impossible to have data of a String object since the term "data" really depends on the creation of the object (during the constructor call).

When you create a String for example, there's a constructor call to String's super constructor - Object. Therefore String has data of Object, and when the Super constructor finishes execution, Strings constructor finishes execution as well so it additional contains data of a String.

This means that String can be supercast into an Object type (for whatever purpose you need to supercast), but an Object cannot always be downcast into a String (because it's possible that the object was derived from something else in which it was never initialized as a String).

-Alex

Technically none of those will work as written, but your original is only wrong by one detail

MySuperType mytype = null;
if (sub_type) {
     mytype = new MySubType (); // this is fine
     mytype.setD(4); // these will fail to compile
     mytype.setE(5);
} else {
     mytype = new MySuperType (); 
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);

If your variable is of MySuperType, then you can only call MySuperType methods on it. To call MySubType methods you will need to cast to MySubType for those calls, like so

MySuperType mytype = null;
if (sub_type) {
     mytype = new MySubType (); 
     ((MySubType)mytype).setD(4); // just fine
     ((MySubType)mytype).setE(5);
} else {
     mytype = new MySuperType (); 
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);

Thanks, Ezzaral. You are always a big help.

It worked. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.