java-Object-oriented article 2

Source: Internet
Author: User

1. Concept of inheritance

    • One of the object-oriented features, you can implement the reuse of the class. The following two ways can be implemented
    • A. Composition/aggregation relationship (contains other classes): Treats a class as a property of another class.
    • B. Derived classes: One class inherits another class, and the class that implements the inheritance is called the derived class, and the inherited class is called the parent class, the base class, and the superclass.
    • The member methods and member variables of the parent class inherit from the quilt class, but the parent class's construction method is not inherited.
    • Only single inheritance is supported in Java, and C + + multiple inheritance is not the same.

2.super keywords

1.super can access member variables of the parent class

2.super can call a member method of a parent class

3.super you can call the constructor of the parent class in the subclass construction method

Note:super can only be used in member methods and construction methods, not in the static method (and this is the same),

Used in the construction method must be placed in the first row.

3.this and Super keyword comparisons (neither can be called in the static method)

4. Method overrides

    1. Method overrides exist only between subclasses and parent classes
    2. When a subclass overrides a method of a parent class, access permissions cannot be scaled down, can be enlarged, and other ways of writing
    3. Static methods of a parent class cannot be overridden by a quilt class as a non-static method

5. Access rights

Access modifiers for class

    1. Public: Classes in any package (that is, all classes) can access the class
    2. Default value: Classes in the same package can access the class

Access modifiers for Members

    1. Private: Only visible to this class, sub-class is not seen
    2. Default value: Visible to classes in the same package
    3. Protected: Visible to classes and all subclasses in the same package
    4. Public: Visible to all classes

Note: Access to members is a prerequisite for accessing the class where members are located

java-Object-oriented article 2

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.