Type checking an init method

Hello Java Gaming world! I have a question about type checking. So let me describe my situation.

I have this interface


public interface State<T extends Entity> {
	
	public void init(T t);
	
	public void update();
	
	public void render(Screen s);
	
	public void processCollision(T t);
	
	public T getEntity();
}

And this class:

public abstract class MobState<T extends Mob> implements State<Mob> {

	protected T m = null;
	
   // more fields defined here...
	
	// some methods defined here...
}

In my code, Mob extends Entity, and MobState implements State. Ball extends Mob, and BallNormalState extends MobState:


public class BallNormalState extends MobState<Ball> {
			
	public void init(Mob m) {
		this.m = (Ball) m;
	}
	
	// more methods defined here...
}

My question is, in BallNormalState::init, I can’t pass anything more specific than Mob because of the interface, so how can I best enforce type checking? It needs to be Ball and not any other type of Mob! Thanks for any advice! 8)

I’d advise that you don’t get yourself wrapped up in unnecessary Generics boilerplate.

What value is this bringing to your code?
Is it reducing, or increasing, complexity & readability?

Maybe I’m over complicating things. I’m trying to come up with something that would be easily reusable in another project and applicable to entities that don’t extend Mob. I’m also in part just experimenting.

Before, I had a State interface and T was just Mob, which worked just fine.

As far as complexity and readability is concerned, I am trying to reduce the former and increase the latter. So what I will probably do is ditch the generic typing in favor of doing just that, because it is rather obfuscating.

public abstract class MobState implements State {
}

Thanks dude, I don’t know why I didn’t catch that.

@Abuse

Generics aren’t necessarily the evil, it is just better to use them internally and not expose them. For example, I have this declaration in my DynamicTree broadphase.


class DynamicTree<AABBType extends DynamicTree.AABB, CollisionType>

That is because I have the same implementation for both 2D and 3D scenes! I just require the AABB type to follow an interface, and CollisionType is simply the component type, CollisionComponent2D and CollisionComponent3D are the actual types that get there.

However, it is bad if you expose this class to the user, as that will make the user to use generics in all the type declarations, so I have wrapper classes DynamicTree2D and DynamicTree3D that I expose, they just use the DynamicTree class internally.