   [java] Accessing an inherited class
  I have a sprite collision manager that stores references to all sprites and detects any collisions by calling 'public Sprite checkCollision()' from the sprite, which returns a reference to any sprite you have collided with. This all works fine but i need to access the inherited classes of Sprite. For example I have a Vehicle class that inherits Sprite and when it collides with another Vehicle i need to get access to the Vehicle.getCollisionDamage() method but all I have is the base Sprite class. Is there any way of accessing an objects methods from its base class? Thanks in advanced.   
  Java uses late-binding, which means that the 'Sprite' reference you pass back in your method will be resolved internally by the JVM to the concrete class it represents prior to executing the method. But that is only true for methods declared in the super class and invoked through a reference of the super class.If you want to call a method exclusive of the concrete subclass, and all you have is a reference of the super class, you'll have to use the 'instanceof' operator, like this:Sprite reference = // ... grab it from somewhere else;if ( reference instanceof Vehicle ) {  ((Vehicle) reference).exclusiveVehicleMethod();}Son Of Cain   ;  if(obj instanceof Vehicle){   ((Vehicle)obj).doSomethingVehicleWise();}orusing(Vehicle v = (Vehicle)obj){   v.doSomethingVehicleWise();}edit:ah, crap, I forget when I'm in the Java vs. the .NET forum.   ;  Got it working now. Thanks for your help   ;Quote:Original post by NightMonkeycalling 'public Sprite checkCollision()' from the spriteHow does a sprite know about any other sprites?Quote:This all works fine but i need to access the inherited classes of Sprite.Having to access inherited classes smells like bad design.Quote:For example I have a Vehicle class that inherits SpriteA sprite is merely a graphical representation of something. Inheriting from Sprite to represent a vehicle does not make much sense to me. What if you decide tomorrow that you want to render your vehicles with OpenGL?;  I have to agree with Fred, there are some design issues in your example.First of all a Verhicle class that extends Sprite doesn't sound logical. A better approach would be to have a Verhicle class that extends/implements a GameEntity class. The Verhicle class would then have the Sprite as one of its members.Second, accessing the inherited classes of Sprite will give you some weird and unmaintainable code. Imagine this:... Somewhere in your gameloop ...if(sprite.checkCollision) {   if(sprite instanceof Verhicle) { ((Verhicle)sprite).getCollisionDamage();   }   if(sprite instanceof Building) { ((Building)sprite).getPaintDamage();   }  }... Somewhere in your gameloop ...Now this doesn't look too bad, but now you get the great idea to add Airplanes, Aliens and Furnature to your game. Instead of simple adding these types, you have to go back to your gameloop and add specific code for these...   ;  I recommend making an interface named CausingDamageAtCollision or something like that, with a getCollisionDamage() function, which all things that make damage when collided with other things will implement. That way, you will only have to do one check for all damage calculations   