Card c; Valentine v; Birthday b; Holiday h c = new Valentine("Debby", 8); //OK b = new Valentine("Elroy", 3); //WRONG v = new Valentine("Fiona", 3); //OK h = new Birthday ("Greg", 35); //WRONG
Here is a picture of another hierarchy: There are no abstract classes in this hierarchy, so each class can be instantiated. As seen previously, the following is OK:
parentReference = childReference
A parent reference variable can hold a reference to an object of one of its child types (or a reference to one of their child types, and so on.) However, the opposite direction
// don't do this childReference = parentReference
can not be done, unless the parentReference actually refers to a child object (this is rare.)
Here are some variables:
Rodent rod; Rat rat; Mouse mou;
Look at the table and decide if each section of code is correct or not.
code section OK or Not? code section OK or Not? rod = new Rat(); rod = new FieldMouse(); mou = new Rat(); mou = new Rodent(); rat = new Rodent(); rat = new LabRat(); rat = new FieldMouse(); rat = new Mouse();