Quick JAVA inheritance question -
if have abstract class named dog constructor set weight (double) , class named specialdog extends dog , has constructor accepts double , passes dog using super().
what (if any) differences between:
dog dog = new specialdog(12.0); specialdog dog = new specialdog(12.0); dog dog = new dog(12.0); specialdog dog = new dog(12.0);
thanks!
to answer questions (they different):
dog dog = new specialdog(12.0);
here, creating specialdog, using dog reference point it. object is special dog, but, unless downcast dog
variable, going able treat dog
dog
, not specialdog
.
specialdog dog = new specialdog(12.0);
here, creating specialdog, , using specialdog reference point it.
dog dog = new dog(12.0);
this compiler error since can't instantiate abstract classes.
specialdog dog = new dog(12.0);
this compiler error. can't assign super class sub class reference. remember: inheritance "is-a" relationship. while specialdog dog, dog might not specialdog.
Comments
Post a Comment