Sign in to follow this  

[java] Accessing an inherited class

This topic is 4206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
if(obj instanceof Vehicle)
{
((Vehicle)obj).doSomethingVehicleWise();
}


or


using(Vehicle v = (Vehicle)obj)
{
v.doSomethingVehicleWise();
}

edit:ah, crap, I forget when I'm in the Java vs. the .NET forum.

Share this post


Link to post
Share on other sites
Quote:
Original post by NightMonkey
calling 'public Sprite checkCollision()' from the sprite

How 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 Sprite

A 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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this