| | |
Run time casting error
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 1,016
Reputation:
Solved Threads: 150
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.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
![]() |
Similar Threads
- Run-time error '5' (Windows NT / 2000 / XP)
- Having problems saving objects into an array, then accessing the data later for repo (Java)
- Run-time Error when printing Array Contents. (C)
- Heap problem at run time (C++)
- "Run time error, do you wish to debug?" (Web Browsers)
- Run-Time Error..? (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Java Eclipse and Wireshark Interaction Help
- Next Thread: Connect to https
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint 2dgraphics android api apple applet application applications arguments array arrays automation bank binary bluetooth chat class classes client code collision component database db development draw eclipse eclipsedevelopment error event exception file fractal game givemetehcodez graphics gui helpwithhomework homework html ide image input integer integration j2me jarfile java javadesktopapplications javafx javaprojects jmf jni jpanel julia learningresources linux list loop map method methods mobile netbeans newbie number object oracle print problem program programming project projectideas recursion researchinmotion scanner screen server service set size sms socket sort sorting sql sqlserver state string swing swt tcp test text-file threads time tree web windows






