Study Summary of Java Learning Note JDK8

Source: Internet
Author: User

Chapter 6 Inheritance and polymorphism6.1 What is inheritance

1. Definition of inheritance: inheritance is the avoidance of repeated definitions of common behavior among multiple classes.

2. Summary: in the textbook through the design of a part of the RPG game code to show us the "Duplicate" code of the shortcomings, in order to improve, we introduced the concept of inheritance. In our self-learning process, through time and again the improvement, has also been the revelation: in different information classes, how many of the attributes set how many variables, give each attribute to set and get the method, since the property can be set to private can still access the reference data, Secondly, when multiple classes have the same properties and the same settings, and get methods, we can use them to refine the code in a way that inherits from the parent class, improving readability and brevity, and not making it easy to modify the code later.

In short, we need to promote the same program code to the parent class by understanding and refining it.

3. Inherited format: We connect the subclass and the parent class by the keyword "extends", which means that the subclass expands and inherits the behavior of the parent class.

4. Introduction of Class Diagrams: Class diagrams are tools for modeling to help us understand the relationship between the contents of a class, the parent subclass, and 161 pages of the textbook, as well as in the Bi Xiangdong Teacher's Java tutorial video.

5.is-a: in inheritance, there is a is-a short interest between the subclass and the parent class, that is, the "is one" relationship. Simply put: A subclass is one of the parent classes. In the textbook, The Sword (subclass) is an example of a role (parent class) to illustrate.

Here is a guide to compiling and running through judgment, which mentions an action: "How to get the compiler to shut up". This is actually a speculative operation, although the compilation can pass without error, but the result of the operation is conceited.

6. Overloading: defining a method with the same name in a parallel class is the use of overloaded methods.

7. Polymorphism: introducing polymorphism is actually an improvement on overloading. When there are many parallel classes and they have common characteristics, we can use them to inherit the modified code and introduce polymorphism on this basis. Polymorphism is actually the use of a single interface to manipulate multiple types of objects. The advantage of this notation is that it can be more maintainable.

8. Redefine: After inheriting the parent class, the same method defined in the parent class is not ripe, but the execution content is different, which is redefined (Override).

Tip: Labeling @override before a method in a subclass indicates that a compiler check is required and that the method is not really redefining a Chinese method of the parent class, but it is not a compile error.

9. Abstract method: If there is no program code operation in a method block, you can use abstract to indicate that the method is an abstract method, which does not have to write {} chunks, to level two with ";" to end.

10. Abstract class: If there is a method in a class that is not operational and is represented as abstract, then this class cannot be used to generate an instance, that is, an abstract class, which is to be expressed in front of class.

11. Inheritance of abstract classes: The Kawai class inherits abstract classes, there are two approaches to abstract methods: one is to continue to mark the method as abstract, and the subclass is therefore an abstraction class that must be preceded by class to represent abstract, and the other way is to manipulate the abstract method. If neither of these practices is implemented, a compilation error is raised.

6.2 Inheritance Syntax Details

1.protected:

(1) The property defined by private cannot be accessed directly in the subclass, but only by obtaining a method. If you want subclasses to have direct access to property variables and do not want to fully open access, you can define them as protected.

(2) is declared as a member of the protected, classes in the same package can be accessed directly, and classes in different packages need to be accessed in the inherited subclass.

(3) When writing a program, if the permission is unknown at the beginning, the member is generally set to private.

2. Redefining the details

(1) Super: If a subclass wants to get a method definition from a parent class, you can add the Super keyword before calling the method. (example see Game6 Role.java; Game6 Swordsman.java; Game6 Magician.java)

(2) Note: The parent class method that can be called with the Super keyword cannot be defined as private. And for method permissions in the parent class, it can only expand but not shrink.

(3) JDK5 improvements: Before JDK5, when redefining a method, in addition to defining a keyword with a greater number of permissions, the other part must be exactly the same as the method signature in the parent class, such as a return value type. After JDK5 and after, as long as the return type is a subclass of the method return type in the parent class, it can also be compiled.

(4) Tip: The static method is owned by the class, and if the same signed static member is defined in the subclass, the member belongs to the subclass, not redefined, and the static method is not polymorphic because the object does not own the static member individually.

3. Constructors:

(1) Precedence of inherited constructor definitions: After creating a subclass of Benji, the initial process defined by the parent class is first performed and the initial process defined in the subclass is performed. That is, after the subclass instance is created, the process defined by the parent class constructor is executed first, and then the process defined by the subclass constructor is executed.

(2) Overloading of constructors: multiple constructors can be overloaded in a parent class, and constructors with no parameters in the parent class are called by default if the subclass constructor does not specify which constructor to execute in the parent class.

(3) This () and super () can only be called, and must be performed on the first line of the constructor.

4.final Keywords:

(1) Final: If the object data member is declared final, but is not explicitly used = specified value, indicating deferred object member value of the specified, in the constructor execution process, it is necessary to have the data member specified worthwhile operation, or compile error.

(2) Final in succession: If the final is used before class, then this class is the last class and cannot be inherited. If the method is limited to final, the subclass cannot redefine the final method.

5.java.lang.object: If a class is defined without inheriting any class using the Extends keyword definition, it inherits Java.lang.Object. That is, it is the topmost parent class of all classes.

6. Garbage collection: For objects that are no longer useful, the JVM has a garbage collection mechanism, and the collected garbage objects occupy a memory space that is freed by the garbage collector. In the execution process, objects that cannot be referenced by variables are garbage objects identified by GC.

7. Abstract class: The emphasis is on examples to embody the idea of succession. (Code package with corresponding code)

Additional: summary supplement

Through my review of Java video, I will record the inheritance with polymorphism-related knowledge points as follows:


1. Advantages of Inheritance:

(1) Improve the reusability of the code. (2) The relationship between classes and classes provides a precondition for another feature polymorphism.

2. The origin of the parent class: in fact, more than one class is constantly extracting the common content. In Java for inheritance, Java only supports single inheritance. Although Java does not directly support multiple inheritance, it retains this multi-inheritance mechanism for improvement.

Single inheritance: A class can have only one parent class. Multiple inheritance: A class can have more than one parent class.

3. Reasons for multiple inheritance are not supported: when a class inherits two parent classes at the same time, the same functionality is found in the two parent classes, which one does the subclass object run when it calls the function? Because the method body exists in the method in the parent class. But Java supports multiple inheritance. A inherits B,b inheritance C,c inherits D. The emergence of multiple inheritance, there is an inheritance system. The top-level parent class in the system is drawn from the constant upward. It defines the function of the most basic and common content of the system. Therefore, a system to be used, direct access to the function of the parent class in the system to know the basic use of the system. When you want to use a system, you need to create objects. It is recommended that you establish the most child class object, because the most subclasses can not only use the functionality in the parent class. You can also use some of the features that are specific to subclasses.

Simply put: For the use of an inheritance system, consult the contents of the top-level parent class to create the object of the bottommost subclass.

4. After the child parent class appears, the characteristics of the members in the class:

(1) member variable: When the same property appears in the child parent class, the object of the subclass type is called, and the value is the property value of the child class. If you want to invoke the property values in the parent class, you need to use a keyword: super.

This: Represents an object reference of this class type. Super: Represents a memory space reference in the parent class to which the subclass belongs.

Note: There is usually no member variable of the same name in the child parent class, because as long as the parent class is defined, the subclass is not defined and inherited directly.

(2) member function: When the same method appears in the child parent class, creating a subclass object runs the method in the subclass. As if the methods in the parent class were overwritten. So this is another feature of the function: Overwrite (duplicate, rewrite). When the functional content of a class needs to be modified, it can be implemented by overwriting.

(3) constructor: When the subclass constructor is found to run, the constructor of the parent class is run first. The reason is that the first row in all the constructors of a subclass has a stealth statement super ();

Note: All constructors in a subclass access the constructor of the null argument in the parent class by default, because the first row within each sub-class construct has the default statement super (); If there is no constructor for the null argument in the parent class, within the constructor of the subclass, you must specify the constructor in the parent class to access through the super statement. If the subclass constructor uses this to specify the calling subclass's own constructor, the constructor that is called will also access the constructor in the parent class.

5. Details of the inheritance:

When the method is covered, note the two point:

When a subclass overrides a parent class, you must ensure that the permissions of the subclass method must be greater than or equal to the parent class method permission to implement inheritance. Otherwise, the compilation fails. When overridden, it is either static or static. (Static can only be overridden by static, or statically overwritten)

6. The disadvantage of inheritance: break the encapsulation. For some classes, or functions in a class, they need to be inherited, or replicated. At this point, the need to introduce final to solve.

7.final Features:

This keyword is a modifier that can be used to modify classes, methods, and variables. The final decorated class is a final class and cannot be inherited. The final modified method is an eventual method and cannot be overridden. The final modified variable is a constant and can only be assigned once.

In fact, the reason for this is to give some fixed data to read a strong name.
Is it possible to use no final decoration? Then this value is a variable that can be changed. Added final, more rigorous procedures. When a constant name is defined, there are specifications, all letters are capitalized, and if it consists of multiple words, the middle is connected with _.

8. Abstract class Abstraction:
Abstract: Not specific, can not understand. Abstract class representation manifests itself.
In the continuous extraction process, the common content of the method declaration extraction, but the method is not the same, no extraction, then the method extracted, not specific, need to be specified by the keyword abstract marked, declared as an abstract method.
The class in which the abstract method resides must be marked as an abstract class, meaning that the class needs to be decorated with the abstract keyword.

9. Characteristics of abstract classes:

Abstract methods can only be defined in abstract classes, and abstract classes and abstract methods must be decorated with the abstract keyword (which can describe classes and methods, and cannot describe variables). Abstract methods define method declarations only and do not define method implementations. An abstract class cannot be created (instantiated) by an object. This subclass can be instantiated only if the subclass inherits the abstract class and overrides all the abstract methods in the abstract class. Otherwise, the subclass is an abstract class.

10. The details of the abstract class:

(1) Is there a constructor in an abstract class? There, used to initialize the subclass object. (2) Is it possible to define non-abstract methods in an abstract class? OK. In fact, there is not much difference between abstract class and general class, it is to describe things, but abstract classes describe things, some functions are not specific. Therefore, both abstract and generic classes need to define properties and behaviors in the definition. However, there is an abstract function more than the general class. And it's a little less than a generic class. The part that creates the object. (3) Abstract keyword and what can not coexist? Final, private, static (4) Can you not define abstract methods in an abstract class? OK. The abstract method is intended only to keep the class from creating objects.

  



Study Summary of Java Learning Note JDK8

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.