The inheritance of Javase Learning notes (v)

Source: Internet
Author: User
Tags define abstract

Inheritance (one of the object-oriented features)

Benefits:

1 : Improved reusability of code.

2 : A relationship between classes and classes that provides a precondition for another feature polymorphism.

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.

Why not support multiple inheritance?

Because when a class inherits two parent classes, the same functionality is available in 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 inherits 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.

After the child parent class appears, what are 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 a property value 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 child class 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 do I use overlays? 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 for the parent class is run first. Why is it ?

Cause: The first line in all the constructors of a subclass has a stealth statement super ();

super (): represents the constructor of the parent class and is used for the constructor in the parent class that corresponds to the parameter. super (): is the constructor that calls the parent-class hollow argument.

Why do I need to call a function in the parent class when the subclass object is initialized? (why join the super () in the first line of the subclass constructor)?)

Because subclasses inherit the parent class, they inherit the data from the parent class, so it is important to see how the parent class initializes its own data. So when the subclass initializes the object, it calls the constructor of the parent class, which is the instantiation of the subclass .

Note: all constructors in a subclass access the constructor of the null argument in the parent class by default, because the first row in 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.

Question:super () and this () can appear in the constructor at the same time.

Two statements can only have one definition in the first row, so only one can appear.

Super () or This (): Why must I define it in the first line?

Because super () or this () is called the constructor, the constructor is used for initialization, so the initialization action is done first.

Details of the Inheritance:

When do you use inheritance?

There is a precondition for inheritance when there is a relationship between classes and classes. A is one of the B's. a inherits B. Wolves are one of the canine branches.

English book, Affiliation: "Isa"

Note: Do not inherit just to get an existing member in another class.

So to judge the relationship, you can simply see, if inherited, the function of the inherited class, can be the subclass has, then the inheritance is established. If not, you cannot inherit.

Detail two:

When the method is covered, note the two point:

1: When a subclass overrides a parent class, it must be guaranteed 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.

2: Overwrite, either static, or not static. (static can only be overridden by static, or statically overwritten )

One drawback of inheritance: breaking the encapsulation. For some classes, or functions in a class, they need to be inherited, or replicated.

Then how to solve the problem? Introduce a keyword, final: Final.

Final Features:

1 : This keyword is a modifier that can be used to modify classes, methods, and variables.

2 : The final Modified class is a final class and cannot be inherited.

3 : The final modified method is the ultimate method and cannot be overridden.

4 : 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 _ .

abstract class : Abstract

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.

Features of abstract classes:

1 : Abstract methods can only be defined in abstract classes, and abstract classes and abstract methods must be decorated by the abstract keyword (which can describe classes and methods, and cannot describe variables).

2 : Abstract methods define method declarations only and do not define method implementations.

3 : Abstract classes cannot be created (instantiated ).

4 : Only the subclass inherits the abstract class and overrides the abstract class. All after the abstract method, the subclass can be instantiated. Otherwise, the subclass is an abstract class.

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?

you can. 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 And what cannot coexist? final, private, static

4 : Can I not define abstract methods in an abstract class? OK. The abstract method is intended only to keep the class from creating objects.

-----------------------------------------------------------------------------------------------

Template Method Design Pattern:

Problem solved: Some implementations are indeterminate when the internal part of the function is implemented. At this point, we can expose the uncertain parts and let the subclasses to achieve them.

Abstract class gettime{

Public final void GetTime () { // This feature can be final qualified If no replication is required

Long start = System.currenttimemillis ();

Code ();/// The functional part of the uncertainty , extracted, implemented by an abstract method

Long end = System.currenttimemillis ();

System.out.println ("Milliseconds is:" + (End-start));

}

Public abstract Void Code ();// Abstract indeterminate function, enabling sub-class replication implementation

}

Class Subdemo extends gettime{

Public Void code (){ // sub-class replication function method

for (int y=0; y<1000; y++) {

System.out.println ("Y");

}

}

}

The inheritance of Javase Learning notes (v)

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.