Run time casting error

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 5
Reputation: acplayer is an unknown quantity at this point 
Solved Threads: 0
acplayer acplayer is offline Offline
Newbie Poster

Run time casting error

 
0
  #1
Jun 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Run time casting error

 
0
  #2
Jun 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,016
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Run time casting error

 
0
  #3
Jun 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: acplayer is an unknown quantity at this point 
Solved Threads: 0
acplayer acplayer is offline Offline
Newbie Poster

Re: Run time casting error

 
0
  #4
Jun 19th, 2009
Originally Posted by JamesCherrill View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,016
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Run time casting error

 
0
  #5
Jun 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: acplayer is an unknown quantity at this point 
Solved Threads: 0
acplayer acplayer is offline Offline
Newbie Poster

Re: Run time casting error

 
0
  #6
Jun 19th, 2009
Originally Posted by JamesCherrill View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,016
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Run time casting error

 
0
  #7
Jun 19th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC