Code
Public classTest { Public Static voidMain (string[] args) {shape shape=NewCircle (); System.out.println (Shape.name); Shape.printtype (); Shape.printname (); }} classShape { PublicString name = "Shape"; PublicShape () {System.out.println ("Shape Constructor"); } Public voidPrinttype () {System.out.println ("This is shape"); } Public Static voidPrintname () {System.out.println ("Shape"); }} classCircleextendsShape { PublicString name = "Circle"; PublicCircle () {System.out.println ("Circle Constructor"); } Public voidPrinttype () {System.out.println ("This is Circle"); } Public Static voidPrintname () {System.out.println ("Circle"); }}
Results
Shape constructorcircle constructorshape This is circleshape
This problem mainly examines the difference between hiding and covering (and of course, polymorphism).
Overrides are only for non-static methods (the end-state method cannot be inherited, so there is a overwrite), and the shadowing is for member variables and static methods. The difference between the 2 is that the overlay is constrained by the RTTI (Runtime type identification) and is hidden from the constraint. This means that only the overlay method will be dynamically bound, and the shadowing will not occur dynamically. In Java, all other methods, except the static method and the final method, are dynamically bound. As a result, the above output will appear.
Java Basics-Overrides-subclasses override methods in the parent class after execution