Hi all,
I am considering whether it is better to have two pointers, one for each object sub class and super, or whether I should just use casting.
How much system resources does this use:
objectName.functionOne();
((SubClass) objectName).functionOther();
Is it better than:
SuperClass objectA = (SuperClass) getSameInstance();
SubClass objectB = getSameInstance();
objectA.functionOne();
objectB.functionOther();
Basically, my main question is about the resources used with casting, versus making an extra pointer. It seems like I could save several in line casts, such as:
((SubClass) objectName).functionOther();
However, is it worth it?
Thanks,
Grae
UPDATE:
There were some unclear parts to my question. Basically, I have a super class that I am using through out a large function. It works with three subclasses. Some the super class is working as I would like. However, I hit a road block in a few places where I have to use a function from one of the three different subclass; a function that is only in one of the subclasses.
I could just have:
SuperClass instanceRef;
SubClass instanceRef2;
instanceRef.etc()
instanceRef.etc2()
instanceRef.etc3()
instanceRef2.specialSubClassOnlyCall();
instanceRef2.specialSubClassOnlyCall2();
or I could have:
SuperClass instanceRef;
instanceRef.etc()
instanceRef.etc2()
instanceRef.etc3()
((SpecialSubClass)instanceRef).specialSubClassOnlyCall();
((SpecialSubClass)instanceRef).specialSubClassOnlyCall2();
However, I don't know which is more efficient.
UPDATE 2:
Here is an example to show you what I am talking about:
class Shape
Triangle extends Shape
Square extends Shape
Circle extends Shape
Cube extends Shape
The Two Pointer Example: (Downside an extra pointer.)
Shape pointer1 = (Shape) getSomeRandomShape();
Cube pointer2 = null;
pointer1.getWidth();
pointer1.getHeight();
pointer1.generalShapeProp();
pointer1.generalShapeProp2();
pointer1.generalShapeProp3();
if(sure_its_cube)
{
pointer2 = (Cube) pointer1;
pointer2.getZAxis();
pointer2.getOtherCubeOnlyThing();
pointer2.getOtherCubeOnlyThing2();
pointer2.getOtherCubeOnlyThing3();
pointer2.getOtherCubeOnlyThing4();
}
Or I could do it this way. (Downside a bunch of casts.)
Shape pointer1 = (Shape) getSomeRandomShape();
pointer1.getWidth();
pointer1.getHeight();
pointer1.generalShapeProp();
pointer1.generalShapeProp2();
pointer1.generalShapeProp3();
if(sure_its_cube)
{
((Cube)pointer1).getZAxis();
((Cube)pointer1).getOtherCubeOnlyThing();
((Cube)pointer1).getOtherCubeOnlyThing2();
((Cube)pointer1).getOtherCubeOnlyThing3();
((Cube)pointer1).getOtherCubeOnlyThing4();
}
So is five casts worse than one extra pointer? What is it was six casts, or 20? Is one cast worse than the pointer.
Grae