This article turns from: http://www.cnblogs.com/happyframework/p/3332243.html, thank you very much.
The rewrite rules in Java are more flexible, as follows:
- All instance methods except the private adornment can be overridden, and no explicit declaration is required.
- Overridden methods to explicitly override this concept, use @Override to annotate.
- Overridden methods can modify access modifiers and return types as long as they are compatible with the methods of the parent class (higher access levels and more specific return types).
- You can use final to mark a method as non-overridable.
- In the construction method, use super (XXX, XXX) to call the parent class construction method, using Super.method (XXX, XXX) in the regular instance method to call the parent class method.
- Java does not support overwrite (new).
public class Test {/*** @param args*/public static void Main (string[] args) {Animal Animal = new Animal (); Animal dog = new Dog (), Animal.say ();d Og.say (); Animal.eat (Animal);d og.eat (dog); System.out.println (Animal.info ()); System.out.println (Dog.info ());}} Class Animal {private String name = "Animal";p rotected Void Say () {System.out.println ("Animal" + "" + THIS.name);} public void Eat (Animal food) {System.out.println ("Animal eat" + food);} Public Object info () {return "Animal";} @Overridepublic String toString () {return "Animal";}} Class Dog extends Animal {private String name = "Dog"; @Overridepublic final void Say () {System.out.println ("dog" + "" + t His.name);} @Overridepublic final void Eat (Animal food) {super.eat (food); System.out.println ("Dog eated");} @Overridepublic final String info () {return "Dog";} @Overridepublic final String toString () {return "Dog";}}
Java-Rewritten Learning