Java object-oriented chapter II

Source: Internet
Author: User

First, the advantages of inheritance

Pros: Improved reusability of code

1, how to judge A, Class B is an inheritance relationship?

Conforms to IS...A relationship

2. In Java, all classes inherit the Java.lang.Object class directly or indirectly.

3. In Java, private members and constructor methods do not inherit the quilt class

A member of the default permission that cannot be inherited by a subclass in another package

Second, how to realize the inheritance

1, the common members of multiple classes are extracted out into a common class (parent, superclass, base class), they only retain the unique members

2. Multiple classes (subclass, derived class), inheriting (extends) public class

Access modifier class Subclass extends parent class {

}

Description: The access modifier in front of class cannot be private, protected

Iii. 2 major features of inheritance

1, singleness: A class can only inherit a parent class directly

2. Transitivity: A class can indirectly inherit multiple classes (subclasses can call the parent class of the parent class ...) Members in the

Iv. how subclasses invoke members of the parent class

1. Public Property

Super. Property name

2. Construction method

Super ("value");

Subclasses implicitly (automatically) find and invoke the parameterless construction method of the parent class

3. Other methods

Super. Method name ([value]);

Third, Method rewrite

1. Concept-Rewrite the method inherited from the parent class.

2. Method overloading (overloading) and method rewriting (overriding) differences and links?

Method Overloads,

In the same class (which can also be in an inheritance relationship), the method name is the same, the argument list (number, type) is different, cannot be different depending on the return type and the access modifier of the method, and the decision is the method overload

Method overrides (method overrides),

is to rewrite the method inherited from the parent class.

When overriding a method, the method name, argument list, return type must be consistent with the parent class method to override, the access modifier can be different, but access must be greater than the parent class

3, Access modifier 4 (by permission from small to large arrangement)

Private < Friendly (default) < protected (protected) < public (common)

4, in the inheritance relationship, constructs the method execution order

1. In inheritance, when a subclass object is created, the parameterless construction method of the parent class is automatically called

2. What is the order in which the subclass and parent classes are executed when they have a constructed method?

When instantiating a subclass object, executes the parameterless construction method in the parent class before executing the construction method in the subclass

3, in the subclass construction method, use the Super keyword to call the parent class with the parameter of the constructor method, the subclass will no longer automatically call the parent class parameterless construction method

Java object-oriented chapter II

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.