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

Using 'OR' and 'AND' in SQL Server -

python - Finding intersection between ellipse and a line -

c++ - NetBeans Remote Development with additional configuration -