Public classBase { Public voidmethod1 () {System.out.println ("Instance method of the parent class"); } Public Static voidmethod2 () {System.out.println ("Static method of the parent class"); } PublicBase method3 () {System.out.println ("Parent class returns a method with a value type of base"); return NewBase (); } Private voidmethod4 () {System.out.println ("Private method of the parent class"); }}
Public classSubextendsbase{//private void Method1 () {//access permissions cannot be strict with the parent class//Public static void Method1 () {//a non-static method of a parent class cannot overwrite a quilt class as a static method Public voidmethod1 () {System.out.println ("Instance methods for subclasses"); } //Public void Method2 () {//static methods of a parent class cannot be overridden by a quilt class as a non-static method//subclasses can define static methods that have the same name as the parent class, so that the static methods of the parent class are "hidden" in the child class Public Static voidmethod2 () {System.out.println ("Static methods for subclasses"); } //The return value type is the same or its child class PublicSub method3 () {System.out.println ("Subclass returns a value of sub method"); return NewSub (); } //the private method of the parent class cannot be overridden by the quilt class, so it can be written, but it is a standalone method Public voidmethod4 () {System.out.println ("Private Methods of subclasses"); }}
15. Method Rewrite rules