Method Hide
A class inherits all non-private static methods from its superclass. The static method of redefining inheritance in subclasses is called method shadowing. A redefined static method in a subclass hides a static method of its superclass. Redefining a non-static method in a class is called a method override.
All rules about method-hidden redefinition methods (name, access level, return type, and exception) are the same as method overrides.
| 123456789101112131415161718192021222324 |
classMySuper { publicstaticvoidprint() { System.out.println("Inside MySuper.print()"); }}classMySubclass extendsMySuper { publicstaticvoidprint() { System.out.println("Inside MySubclass.print()"); }}publicclassMain { publicstaticvoidmain(String[] args) { MySuper mhSuper = newMySubclass(); MySubclass mhSub = newMySubclass(); MySuper.print(); MySubclass.print(); ((MySuper) mhSub).print(); mhSuper = mhSub; mhSuper.print(); ((MySubclass) mhSuper).print(); }} |
The above code produces the following results.
Click on the link to view the details
Java Inheritance method Hide (overwrite)