1.2Java Basic Knowledge---inheritance

Source: Internet
Author: User

More details can be seen http://www.codeceo.com/article/java-extends.html

1. What is inheritance?

This is a Java class relationship, is a parent-child relationship, when we say that Class B inherits Class A, when class B has the characteristics of Class A (preserving methods and properties), we call Class A A parent class, Class B is a subclass

That simple understanding example, like the car class is the parent class, it has a subclass of BMW.

So let's take a look at the simple code, the inherited keyword is extends

// BMW is a subclass, car is the parent class class extends car{}

2. Characteristics of inheritance
(1) Subclass inherits methods and properties of parent class

(2) Subclasses can override methods that inherit from a parent class

It is worth noting that the return parameters and incoming parameters of the method should be consistent at this moment

(3) Single inheritance: A subclass can have only one parent class

4. Method rewriting and refactoring
If a method is defined in a subclass whose name, return type, and argument list match exactly the name, return type, and argument list of a method in the parent class, the subclass's method overrides the parent class's method.
Methods override in different classes and are necessary for implementing polymorphism.

Overloading is used in the same class where the method names of the methods are the same, and the argument list is different (not related to the return value type).

5. This and super in succession:

The This in the constructor represents the object reference that is currently being initialized, and this in the method represents the object reference that is currently calling this method. This specific usage is shown in a few ways:

1. When there are multiple overloaded constructors, and one constructor needs to call another to construct it, it is called with this (Param) in its first row and only in the first row;

2. When a method in the object needs to call the other methods in this object, use this as the keynote, or do not write, in fact, the default is this as the keynote;

3. When the object property and the local variable name in the method are the same, in this method you need to explicitly use this as the keynote to represent the properties of the object, if this problem does not exist, you can not explicitly write this.

In fact, one of the problems involved is the search rule for variables: first local variables, and the variables defined in the current class, the variables that can be inherited by the quilt class defined in the parent class, and the parent class ...

Super represents calling the corresponding properties and methods in the parent class. In the method, if you need to call the method of the parent class, be sure to write it in the first line

  

1.2Java Basic Knowledge---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.