201671010116.2016-2017-5 "Java Programming" understanding inheritance

Source: Internet
Author: User
Tags control characters modifier

This week in the teacher's explanation I can find myself before the details of self-study and easy to confuse the concept of self-study after the teacher explained, a lot of problems will be solved. For example, after the teacher explained, I have more knowledge of inheritance:

1. Parent class:

1) A class in Java can have more than one child class, but a class may have a parent class, and a parent class can have a parent class, which supports so-called single inheritance and multi-tier inheritance, but not multiple inheritance.

2) All classes in Java have methods and properties of the object class. Due to the methods and properties of the Quilt class inheritance, many methods quilt class rewrite, commonly used are:

Tosrting: The method returns the string representation of the object by displaying the information about the class after the subclass is rewritten;

Equals: Compares two objects for equality by overriding them in subclasses;

Hashcode: Returns the hash code of the object, the hash code is not regular.

2. Sub-Category:

1) Memory Format: Memory overlay

2) in inheritance, subclasses can inherit all the properties and methods of the parent class, but cannot inherit the parent class's constructor, which means that the constructor of the parent class cannot be overridden.

3) Super (parent class of the current object):

    • On the first line of the construction method
    • When the constructor is called, the format is super (), and the constructor that is set in the parent class is called
    • When calling normal methods and properties, the format is super and can only be properties and methods of the parent class.
    • When a subclass is overriding a method of a parent class, you must use the Super keyword if you want to invoke the method of the parent class

4) This (current object):

    • On the first line of the construction method
    • When the constructor is called, the format is this (), and a constructor for this class in this class is called
    • When calling normal methods and properties, the format is the property/method of the This parent class/subclass
    • When a subclass is overriding a method of the parent class, if the This keyword is used, the method of this class is called

3.final:

1) Variable: Indicates that the variable cannot be modified, that is, constant.

2) Class: Indicates that the class cannot be inherited and is the final class, that is, the class cannot have subclasses. At the same time, if it is declared as the final class, it is generally preceded by the class keyword with final

3) Method: Indicates that the method cannot be overridden, and if the method is declared as the final method, it is generally put final after the access modifier, before the return value.

4. Object initialization:

1) In the object initialization phase, both properties and methods are intended for properties and methods that subclasses can inherit from the parent class, generally in the case of non-private to the parent class. Because the private adornment is unique to the parent class, the subclass does not inherit, and when the new subclass does not need to allocate space for it and perform initialization. Of course, the constructor subclass of the parent class is not inherited, but the constructor is a matter of other kinds.

2) class initialization is performed only once, and when multiple objects are new to the same class, class properties and class initialization blocks are initialized only once.

5. Shadowing in Inheritance:

The Java class has three access controls: private, protected, and public, and is represented as a default access control state when the three access control characters are not written. Therefore, there are four levels of access control altogether. The specific access control performance is as follows:

1) The property or method of private modification is unique to the class and cannot be accessed directly in any other class;

2) The property or method of the default modifier has a package access attribute, and other classes in the same package can be accessed;

3) Protected modified properties or methods can be accessed by other classes in the same class, and also accessible to subclasses that are not in the same package;

4) Public-decorated properties or methods can be accessed directly in the outer class.

When a subclass inherits the parent class, the subclass can inherit the properties and methods (generally non-private adornments) in the parent class that have access control permissions, and the subclass does not inherit from the properties and methods that are unique to the private decorated parent class.

When a subclass needs to change the inherited method, it is often said to override the parent class. Once overridden, this method of the parent class behaves as hidden for the subclass. After the object of the subclass calls this method, it is called after the subclass is overridden

method, but there are two ways in which you can invoke the original method of the parent class in the subclass object:

1) The Subclass object type is coerced into the parent class type, making the call;

2) via Super call.

Similarly, if a property of the same name in the parent class is defined in a subclass, the parent class property is represented as hidden in the subclass.

201671010116.2016-2017-5 "Java Programming" understanding inheritance

Related Article

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.