In my application I have:

Class B

Class C extends B. Class C contains one method that overrides a method in B. Lets call the method myMethod and Class C has the constructors that simply call the super method in the parent class.

In a JSP file that uses these objectsI have an object: ob and the following code:

if (some_condition) {

//Here I want to accomplish the following:

((C)ob).myMethod();

// That is I want to execute the method as if the ob was actually of class C.

}

I can code it to avoid compilation problems as above. But I keep getting a cannot CAST from B to C runtime error.

Any ideas?

The high level puprose of this is that there is one type of user of the application who has some special code they want to execute. And for obscure reasons the company does not want to put conditional code in the code for class B.

Can you move the conditional portion into "C" and just leave the call as "ob.myMethod()"? You can't expect to upcast a "B" object to a "C". If you can't let the conditional be handled polymorphically, you'll have to add an "instanceof" test prior to the upcast and method invocation.

Beware the trap of confusing a reference variable (ob) with the object it happens to refer to at any particular time. How is ob declared? If its declared as type B then you shouldn't need to do anything. ob can refer to an object of class B or C, and if it happens to be of type C the right version of the overridden method will be called automatically. If the object referred to really is of type B then trying to call a method from sub-type C is surely a really bad design and needs to be re-thought.

Beware the trap of confusing a reference variable (ob) with the object it happens to refer to at any particular time. How is ob declared? If its declared as type B then you shouldn't need to do anything. ob can refer to an object of class B or C, and if it happens to be of type C the right version of the overridden method will be called automatically. If the object referred to really is of type B then trying to call a method from sub-type C is surely a really bad design and needs to be re-thought.

Yes. I agree.

Here Ob is of type B and is instantiated as such elsewhere in the code. We dont want to put the new logic in the code for B (as it is for a one time customer) and have been looking for ways to achieve the objective. This was one floated suggestion but I have demonstrated that it is not feasible and as you say a bad idea in general.

I would like to overload the method in class B but the idea of touching class B is getting nowhere here. Guess it is time for further push back.

Can't you instantiate this particular object as type C wherever it is instantiated? This would be good OO practice - subclass to handle a special case, and once instantiated correctly the rest would be automatic.

Can't you instantiate this particular object as type C wherever it is instantiated? This would be good OO practice - subclass to handle a special case, and once instantiated correctly the rest would be automatic.

I could but..

The code is littered in many files with instanceof checks (class B has many siblings) and that change would impact half a dozen or more files.

This is a 8 year old system, touched by many hands, that merges two products into one and I have just picked up support for it and... many things.

I will discuss this with others and either instantiate correctly or overload class B. I think those are my options.

Here's my submission for hack of the month:
Make C the superclass of B. Then B will continue to work and be usable as before, but you can call super.myMethod() for your special case.
Don't tell me this is tacky, I know.

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.