Java down casting
public class TInq extends Inq {
private String tsyInq;
public String getTsyInq() {
return tsyInq;
}
public void setTsyInq(String tsyInq) {
this.tsyInq = tsyInq;
}
}
public class Test {
public static void main(String[] args) {
Inq inq = new Inq();
inq.setId("123");
inq = new TInq();
TInq tinq = (TInq) inq;
}
}
This works because supercalss (Inq) is infact holding refrence to sub class. If i remove highligted line then one would get cast class exceptions.
public class TInq extends Inq {
private String tsyInq;
public String getTsyInq() {
return tsyInq;
}
public void setTsyInq(String tsyInq) {
this.tsyInq = tsyInq;
}
}
public class Test {
public static void main(String[] args) {
Inq inq = new Inq();
inq.setId("123");
inq = new TInq();
TInq tinq = (TInq) inq;
}
}
This works because supercalss (Inq) is infact holding refrence to sub class. If i remove highligted line then one would get cast class exceptions.
No comments:
Post a Comment