Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6. What is a significant difference between C++ and Java inheritance? Solution >

ID: 3655888 • Letter: 6

Question

6. What is a significant difference between C++ and Java inheritance?

Explanation / Answer

> > Is their anyway (through either inheritance or interfaces) > > to declare an abstract static method? I would like > > to use polymorphism for several classes and I would > > like all of them to have static method. > > I know I could do this by having the static method (not > > abstract) in the super class and then overriding the > > method in the subclasses, but I would like to have the > > method abstract so it would be required to rewrite the > > method to create the subclass. > I'm afraid you can't always get what you want. Java has no > abstract static methods. Abstract methods are bound up in > the polymorphism thing. They are intended to provide > templates for subclasses not to simply "rewrite" but to > "override" -- so that when they are invoked from a superclass > reference type, dynamic binding causes the appropriate > subclass implementation of the method to run. Static methods > are bound at compile time based on the type of the reference, > not at run time based on the class of the object. When you > rewrite a static method in a subclass, you aren't "overridding" > the superclass version of the method, but simply "hiding" it. > But if you invoke the method on a superclass type reference to > the subclass object, you'll still just get the superclass > implementation of the method. Even though your holding a > reference to a subclass object, the static method > implementation to invoke is decided at compile time based on > the type of the reference, which is the superclass.