Home > Java > javaTutorial > body text

Java-object-oriented-inheritance, polymorphism, composition

高洛峰
Release: 2016-11-17 14:17:08
Original
1130 people have browsed it

Characteristics of inheritance

Single inheritance: Each subclass has at most one direct parent class. Note that it is the direct parent class. The number of indirect parent classes is not limited.

Pay attention to the concept of parent class: A-->B--> C-->D, here, ABC is the parent class of D, C is the direct parent class of D, and AB is the indirect parent class of D

The relationship between parent class and subclass is general and special; subclass extension Without the parent class, the subclass is a special parent class

Object is the direct or indirect parent class of all classes; when defining a class, if the parent class is not directly specified, the Object class will be inherited by default

The subclass inherits from the parent class What elements are inherited

The subclass does not inherit the constructor method of the parent class, but it always calls the constructor method of the parent class

The subclass inherits the instance variables and instance methods, class variables and class methods of the parent class. Of course, the premise is that there are no Modified by private

Members modified by private in the parent class will not be inherited by the subclass

Methods modified with final in the parent class cannot be overridden by the subclass

Override of the method

Owned by the subclass and the parent class The same method signature is called a subclass that overrides the method of the parent class

Two same, two small, one big principle:

Two same: the method name is the same, the formal parameter list is the same

Two small: the return value of the subclass method is the same as The parent class's equal or smaller; the exception thrown by the subclass method must be equal to or smaller than the parent class's exception. One big one: the access rights of the subclass's method must be equal to or larger than the parent class's. static: The subclass method must be consistent with the parent class method, either a class method or an instance method. The two cannot be inconsistent. After the method is rewritten, the subclass object will not be able to access the parent class method, but it can be called in the subclass method. Parent class methods: super. instance method, parent class name. class method

If the parent class method is privately modified, the subclass cannot override the method; if the subclass has a method with the same signature as the parent class private method, This is not rewriting, it is a newly added method of the subclass and has nothing to do with the parent class. Subclass methods may be overloaded with the parent class methods. Methods modified with final in the parent class cannot be overridden by the subclass. Write the

super keyword

In the subclass, use the super keyword to call the instance variables or instance methods of the parent class

super and static cannot be used to modify methods at the same time; just like this and static cannot modify methods at the same time

The subclass inherits a variable from the parent class and defines another variable with the same name. At this time, you can use super.variable name or parent class name.variable name to access the variable of the parent class

The search order of variables

If a subclass method accesses a variable, without explicitly specifying the caller, search in the following order:

Is there a local variable with the same name in the current method?

Is there a member variable with the same name in the current class

In the direct parent class, is there a

traceable online step by step? If it is not found in the end, it will prompt a compilation error

super calls the parent class constructor

Although the subclass will not inherit the parent class's constructor, it will always call it The constructor of the parent class. To call the constructor of the same class, use this, and to call the parent class, use super

Explicit call: the super call statement is written on the first line of the constructor method, because this must also be written on the first line, so super and this will not Appears at the same time

Indirect call: The first line of the subclass method is this, and the called constructor must also call the constructor of the parent class

Implicit call: No super, no this, the system defaults to calling the parent class's constructor. Parameter structure

The subclass inherits the parent class, and the parent class constructor will always be called level by level until Object is called, and after finding the top parent class constructor, it will start executing layer by layer

For example, the following code, inheritance relationship: A-->B-->C-->D

public class Test{  
    public static void main(String[] args) {  
        D d=new D();
    }
} 
class D extends C{    D(){
        System.out.println("D类构造器");
    }
}class C extends B{    C(){
        System.out.println("C类构造器");
    }
}class B extends A{    B(){
        System.out.println("B类构造器");
    }
}class A{    A(){
        System.out.println("A类构造器");
    }
}
Copy after login

Output:

A class constructor

B class constructor

C class constructor

D class constructor

If the parent class does not have a parameterless constructor, and the subclass needs to call the parent class’s parameterless constructor, then it cannot be compiled.

Polymorphism

Simply put, polymorphism is: in a pyramid-style inheritance system, When creating bottom subclass objects, use the top parent class type to point to these bottom subclass objects. When the same method is called through the same type of reference variable, different results will appear


Polymorphism comes from: inheritance + parent type The reference points to the object of the subtype + the overriding of the method

The reference variable has two types, one is the compile-time type and the other is the runtime type

Upward transformation: The parent type reference points to the subtype object, and the upward transformation is automatically completed by the system

In the case of upward transformation: you can call the overridden method of the subclass through the reference of the parent type, but you cannot access the instance variables in the parent class that are not found in the subclass, which means that the instance variables do not have polymorphism

instanceof and forced type conversion

Basic types have forced type conversion, such as (double)16; reference types also have forced type conversion

For example, there is the following inheritance chain: A-->B-->C--> ;D-->E-->F, B b=new D(). At this time, the B type is used to point to the D object. At this time, the b variable can be converted to the ACD type, but it cannot be converted to the EF type. Throws Exception ClassCastException

Before performing forced type conversion, you should first use instanceof to determine whether the conversion can be performed to avoid throwing exceptions

X variable instanceof Y type: Determine whether the X variable can be converted to Y type. There may be three situations:

true means yes Turning means that the type of the object pointed to by the The type of the object pointed to by the X variable has no parent-child or child-father relationship with the Y type. Both are sub-types of the

Inheritance can achieve code reuse, but destroys encapsulation; combination can also achieve code reuse

Principles to be followed when designing parent classes:

Try to use private to hide the internal data of the parent class and prevent direct access by subclasses Member variables of the parent class

Do not allow subclasses to access or modify parent class methods at will. The auxiliary tool methods of the parent class are decorated with private; the methods that need to be called from the outside are public; the methods that do not want to be rewritten by subclasses are decorated with final; if you want to be rewritten by subclasses but do not want external access, use protected

No Calling methods that may be overridden by subclasses in the parent class constructor will have serious consequences

Principles of design inheritance:

Subclasses need to add additional attributes, not changes to attributes

Subclasses need to add their own unique For functions or behaviors, you can add new methods, or override parent class methods

Don’t design inheritance just for the purpose of code reuse, but look at the actual situation, whether the parent class and subclass have a "has-a" relationship

Don’t want a class to be inherited:

final: Modify the class with final as the final class and cannot be inherited

private: Modify all the constructors of the class with private, so that subclasses cannot call the constructor of the class. It cannot be inherited. In addition, a static method should be provided to return objects of this class. Combination can also realize code reuse. Class A reuses the methods of class B. Create objects of class B in class A. And use private modification

Call the method of class B object in class A method

Combination or inheritance:

Inheritance: The subclass and parent class have an "is-a" relationship, the relationship between concrete and abstract, special and general

Combination: The new class and the old class have a "has-a" relationship, the relationship between the whole and the part

Others:

For the case where the subclass inherits a variable from the parent class and defines a variable with the same name, when creating the subclass class object, memory is allocated for both variables, but the parent class variable is hidden

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!