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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -