Summary of Overrides in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.