.
Tree
/ \
/ \
/ \
Redwood Blackwood
:
:
from the sub classes towards the root.
:
A Redwood Blackwood tree: So
Tree t1;
Tree t2;
Redwood r = new Redwood() ;
Blackwood b = new Blackwood() ;
t1 = r; // Valid
t2 = b; // Valid
Downcast:
from the root class towards the children or subclasses. .
:
Redwood r = new Tree(); //compiler error, because Tree is not a Redwood
Blackwood r = new Tree(); //compiler error, because Tree is not a Blackwood
a Tree object , Redwood Blackwook, - .
:
Tree t1;
Tree t2;
Redwood r = new Redwood() ;
Blackwood b = new Blackwood() ;
t1 = r; // Valid
t2 = r; // Valid
Redwood r2 = (Redwood)t1;
Blackwood b2 = (Blackwood)t2
[]
Redwood r = (Redwood) new Tree(); ?
Downcast:
source Redwood r = (Redwood) new Tree(); fist Tree Redwood.
:
Tree t = new Tree();`
Redwood r = (Redwood)t;
, ,
[]
?
Redwood tree supper. .
t = ();
t Tree() Redwood(). .
, t, . - Redwood r = (Redwood)t;, t Tree class, .
[:]
instanceof:
casting/coercing , instanceof . *
:
instanceof . , , . :
if (t instanceof Redwood)
{
Redwood r = (Redwood)t;
}
: - .
: instanceof