java--inheritance

Source: Internet
Author: User

First, the concept

  In the program, inheritance describes the relationship between things, through inheritance can make a relationship between the various things system.

Format: Class Subclass extends parent class

Second, the characteristics

1, in Java, the class only supports single inheritance, does not allow multiple inheritance, a class can only have a direct parent class, for example, the following case is not legal

Class a{} class b{} class C extends a,b{}

2. Multiple classes can inherit a parent class for example, the following conditions are allowed

Class a{} class B extends a{} class C extends a{}

3. in Java, multi-layer inheritance is possible, that is, the parent class of a class can inherit another parent class, for example, Class C inherits from Class B, and Class B can inherit Class A, at which point the Class C can also be called a subclass of Class A. The following scenario is allowed.

Class a{} class B extends a{} class C extends b{}

4. in Java, subclasses and parent classes are a relative concept, meaning that a class is a class parent class, or it can be a subclass of another class. For example, in this case, Class B is a subclass of Class A, and the parent class of Class C

Third, the characteristics of the member variables in the inheritance

  1. Subclasses can only get members of the parent class that are not private; the member variable name in the child parent class directly gets the member variable of the parent class, and the member variable name in the child parent class gets the member variable of the child class.

2, the nearest principle: Who is close to me who, if there are local variables with local variables; there are no local variables, there are child class member variables with the child class member variables; no local variables and subclass member variables are used with parent class member variables.

3. Super can get the member variables and methods of the parent class.

Iv. Method Rewriting

Application Scenarios:   when the method of the parent class does not fully satisfy the subclass, it can preserve the function of the parent class (lineage, inheritance), and can have its own unique function

Precautions : You cannot override a private member method of a parent class, and you cannot see the private member of the parent class at all

Subclasses override parent class methods, permissions must be greater than equal to the permissions of the parent class method

Note: @Override: Method Overrides, which describe methods that override the parent class.

Wu, This&super

  A:super (argument list); statements are used in the construction method of a subclass to invoke the constructor method in the parent class (which is determined by the parameters passed) and can only be used in the first row of the constructor method

B:this (argument list );  Statement Used in the constructor of a class to invoke other constructor methods in this class, which are determined by the parameters passed, and can only be used in the first row of the constructor method

Vi. advantages and disadvantages of inheritance

  A: Advantages

Improved reusability of the code

Improved maintainability of the code

B: Disadvantages:

The coupling of the classes enhances the

Principles of development: cohesion-poly low coupling

Cohesion: the ability to do something yourself

Coupling: Class-to-class relationships

    

java--inheritance

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.