It's easy to understand.
I will directly put a program, the program has instructions!!
Parent class:
1 PackageParentandson;2 3 Public classParentextendsObject {//4: Final decorated class cannot inherit, 3:object is a direct parent or indirect parent class for all classes4 5String sex= "Female";6 String name;7 8 9 PublicParent () {Ten One } A - PublicParent (String sex, string name) { - This. Sex =sex; the This. Name =name; - } - - protected StaticObject Say (String name) { +System.out.println ("Father Speaks"); - return0; + } A Public voidWalk () { atSystem.out.println ("Father walking"); - } -}
Sub-class:
1 PackageParentandson;2 3 Importjavax.management.remote.SubjectDelegationPermission;4 5 //1: Inheritance is a mechanism for creating classes based on existing classes6 Public classSonextendsParent {//: 2:java only supports single inheritance7 8 String age;9 Ten PublicSon () { One Super(); A } - - the PublicSon (String age) { - Super(); - This. Age =Age ; - } + - //@Override + /*1: Override requires method name and method parameter list (the number of parameters and the data type of the parameter) must be the same A * 2: Overridden return value A: if it is empty or basic data type, the subclass return value is identical to the parent class at * B: If it is a reference data type, the subclass returns a value type like the parent class or his subclass - * 3: Access rights, the subclass has the same access rights as the parent class, or is greater than the access rights of the parent class - * 4: The method in the parent class that is modified by the static final modifier can be inherited by the quilt class, but cannot be overridden, @Override can determine whether the method is overridden or a new method - * 5: The parent class is not a static method, and the subclass cannot be added static - * - */ in Public StaticInteger Say (String name) { -System.out.println ("The Son Speaks"); to return0; + } - //@Override error because the Dohomework () method is not an overridden method the Public voiddohomework () { * System.out.println (sex); $System.out.println ("Son do homework");Panax Notoginseng } - Public Static voidMain (string[] args) { theSon son =NewSon (); + System.out.println (son.sex); A } the}
Summary of Overrides in Java