J
jeffc
Reading Mughal "Programmer's Guide to Java Certification". It talks about
widening and narrowing conversions of primitive data types. Widening is OK
(e.g. int to float). Narrowing causes loss of information. This makes sense.
Now the confusing part, wrt reference types. It also calls upcasting a widening
conversion, and downcasting a narrowing conversion. This makes sense from the
perspective that going up the class hierarchy is usually fine, but going down is
usually not. What doesn't make sense is the "widening/narrowing" terminology,
and the loss of information. You are actually widening going down the
hierarchy, in terms of data size. You are also losing information, in a sense,
by going up the hierarchy. (After upcasting, you no longer have access to the
class extensions, so your object is effectively smaller, not larger.
Comments?
widening and narrowing conversions of primitive data types. Widening is OK
(e.g. int to float). Narrowing causes loss of information. This makes sense.
Now the confusing part, wrt reference types. It also calls upcasting a widening
conversion, and downcasting a narrowing conversion. This makes sense from the
perspective that going up the class hierarchy is usually fine, but going down is
usually not. What doesn't make sense is the "widening/narrowing" terminology,
and the loss of information. You are actually widening going down the
hierarchy, in terms of data size. You are also losing information, in a sense,
by going up the hierarchy. (After upcasting, you no longer have access to the
class extensions, so your object is effectively smaller, not larger.
Comments?