In depth understanding of Java constructor method

  • 2021-11-13 07:35:14
  • OfStack

Directory overload 1. Constructor overload 2. Method overload (overload) override

Heavy load

1. Overloading the constructor

Because the constructor name must be the same as the class name, all constructor names of the same class must be the same, which constitutes an overload; In order for the system to distinguish different constructors, the parameter lists of multiple constructors must be different.


class Person{
    int age;
    String name;
    public Person(){
    }
    public Person(int age){
        this.age = age;
    }
    public Person(int age,String name){
        this(age);
        this.name = name;
    }
}

2. Overloading of methods (overload)


1. Definition: In the same class, more than 1 method with the same name is allowed, as long as their parameter number or parameter type is different.

Two identical and one different ": the same class and the same method name

Different parameter lists: different number of parameters and different parameter types

2. Examples:

Overloaded sort ()/binarySearch () in the Arrays class

3. Determine whether to overload:

It has nothing to do with the permission modifier, return value type, formal parameter variable name and method body of the method!


class Person{
    public void getSum(int i,int j){//A    
        System.out.println(" Han Han ");
    }
    public void getSum(double d1,double d2){//B
    }
    public void getSum(String s,int i){// Different order of formal parameters also constitute overloading //C
    }
    public void getSum(int i,String s){//D
    }
    public void getSum(int i,int j){// Has nothing to do with the method body! ! ! ! 
        return 0;
    }
    public void getSum(int m,int n){// It has nothing to do with the parameter name! ! ! ! 
    
    }
    private void getSum(int i,int j){// Permission modifier size independent! ! ! ! 
    
    }
}

4. How to determine a specified method when calling a method through an object:

Method name---- > Parameter list

Rewrite

Why override: The function of the parent class cannot meet the needs of the subclass

The premise of method override: There must be an inheritance relationship!

Application: After overriding, when the subclass object is created, when the method of the parameter with the same name of the subclass parent class is called through the subclass object, the subclass overrides the method of the parent class when it is actually executed

Rewritten provisions:

Method declaration: Permission modifier return value type method name (formal parameter list) throws exception type {

//Method body

}

Conventions are commonly known as overridden methods in subclasses and overridden methods in parent classes

A. The method name and parameter list of the method overridden by the subclass are the same as the method name and parameter list of the overridden method of the parent class

B. The permission modifier of the method overridden by the subclass is not less than the permission modifier of the method overridden by the parent class

> Special case: Subclasses cannot override methods declared as private permissions in parent classes

C. Return value type:

> If the return value type of a method overridden by a parent class is void, the return value type of a method overridden by a subclass can only be void

> If the return value type of the method overridden by the parent class is A, the return value type of the method overridden by the subclass can be A or a subclass of A


// This is a rewrite, String Yes Object Subclass of 
pulbic Object show(){// Parent class method 
}
public String show(){// Subclass method 
}

// This is not an override, and the return value type is different 
pulbic void catch(){// Parent class method 
}
pulbic int  catch(){// Subclass method 
}

> The return value type of the method overridden by the parent class is a basic data type (for example, double). Then the return value type of the method overridden by the subclass must be the same (double)

D. A method overridden by a subclass throws an exception type not greater than the method overridden by the parent class. (Fewer exception types can be thrown, smaller exception types can be thrown, and no exception can be thrown)

Methods with the same name and parameters in both subclasses and parent classes are either declared as non-static (overrides considered) or static (not overrides).

Note: Static methods of A parent classes cannot be overwritten as non-static methods by subclasses. A static method of an B parent class cannot be overwritten as a static method by a subclass. (These two points have the same meaning as the above sentence)

E. Static methods cannot be overridden

Reason: Overrides depend on class instances, and static methods have nothing to do with class instances. And static methods have nothing to do with class instances. Moreover, static methods are determined at compile time, while method overrides are determined at run time (dynamic binding). (It can also be said that java polymorphism is manifested at runtime, while static is contrary to it at compile time.)

"JAVA Programming Idea" mentioned many times that if a method is static, its behavior will not be polymorphic. Static methods are associated with classes, not individual objects.

Distinguish overloading and overriding of methods

Overload: It does not show polymorphism.

Rewrite: It is polymorphic.

From a compile and run perspective:

Overload means that multiple methods with the same name are allowed, but these methods have different parameters. The compiler modifies the name of the method with the same name according to different parameter lists of the method. To the compiler, these methods with the same name become different methods. Their calling address is bound during compilation. The overload of Java can include a parent class and a child class, that is, the child class can overload the method of the parent class with the same name and different parameters.

So: For overloads, the compiler has already determined the method to be called before the method is called, which is called "early binding" or "static binding";

For polymorphism, the interpreter will not determine the specific method to be called until the moment when the method is called, which is called "late binding" or "dynamic binding";

To quote an Bruce Eckel: "Don't be silly, if it's not late binding, it's not polymorphic."

Rewrite rule supplement

1. The abstract methods of the parent class can be overridden by the child class in two ways (i.e., implemented and overridden).

2. The non-abstract method of the parent class can be overridden as an abstract method (in this case: the subclass must be an abstract class).


Related articles: