we can achieve output in 2 ways 1 typecasting , 1 without typecasting
a a=new b() // without typecaste a = (a)a// typecaste
in both ways same output.so, use of typecasting
let's assume have list of animal
s. , have tiger
s , lion
s in it.
arraylist<animal> animals = new arraylist<>(); //add tigers , lions //sort tigers @ beggining of list tiger t = (tiger)animals.get(0);
without casting type missmatch @ compile time. cast risk classcastexception
can easy caught try-catch
it's example of proper use of class casting in java.
Comments
Post a Comment