Force Java upper cast class -


it possible force upper cast type in java? see example.

public class animal{}  public class dog extends animal{}  dog dog = new dog();   animal test = dog; 

what have: (test instanceof dog) , (test instanceof animal) return true.
want: (test instanceof dog) == false , (test instanceof animal) == true

there way force test animal? or way creating constructor animal passing dog? trying avoid create constructor because had complex object, lot of getters/setters.

thanks.

i don't know why care if object animal not dog, after all, dog animal. can apply dog should applicable animal, else there design issue.

but if must determine that, call getclass() (defined object) class object, compare animal.class.

if (animal.class == test.getclass()) 

Comments

Popular posts from this blog

java - How to Configure JAXRS and Spring With Annotations -

visual studio - TFS will not accept changes I've made to a Java project -

php - Create image in codeigniter on the fly -