downcast

Is it possible to avoid a downcast?

I have some logic, which defines and uses some user-defined types, like these: class Word { System.Drawing.Font font; //a System type string text; } class Canvass { System.Drawing.Graphics graphics; //another, related System type ... and other data members ... //a method whose implementation combines the two System types in...

How to change this design to avoid a downcast?

Let's say I have a collection of objects that all inherit from a base class. Something like... abstract public class Animal { } public class Dog :Animal { } class Monkey : Animal { } Now, we need to feed these animals, but they are not allowed to know how to feed themselves. If they could, the...

Why can we cast a Java interface to *any* non-final class?

import java.util.Collection; public class Test { public static void main(String[] args) { Collection c = null; Test s = null; s = (Test) c; } } In the code sample above, I am casting a collection object to a Test object. (ignoring the null pointer). Test has no relationship to Collection whatsoeve...

C++ inheritance question

Hello! I have the following problem in application architecture and am willing to solve it (sorry for a lot of text). I am building a game engine prototype and I have base abstract class AbstractRenderer (I will use C++ syntax, but still the problem is general). Assume there are some derived implementations of this renderer, let's say...