Study notes: java overloading and construction method

Method overloading is a feature. If a class has different parameter lists, it allows a class to have multiple methods with the same name. It is similar to constructor overloading in Java, which allows a class to have multiple constructors with different parameter lists.
Note that this is wrong

int add(int, int)
float add(int, int)

For example

public class Person {
    
    
    private String name;
    private int age;
    private Date birthDate;
    public Person(String name, int age, Date d) {
    
    
 	this.name = name;
 	this.age = age;
 	this.birthDate = d;
     }
     public Person(String name, int age) {
    
    
 	this(name, age, null);    
	//this.name=name; this.age=age; this.birthDate=null;
     }
     public Person(String name, Date d) {
    
    
 	this(name, 30, d);	   	
	//this.name=name; this.age=30; this.birthDate=d;
     }
     public Person(String name) {
    
    
	 this(name, 30);	   //this.name=name; this.age=30;
     }
}

The difference between overloading and rewriting in Java
Overloading
methods have the same name, and the types or numbers of parameters are different.
There is no requirement for permissions.
Occurs in a class.
Overriding
method names, parameter types, and return value types are all the same.
The overridden method cannot have stricter permissions than the parent class.
When the new object is created, the constructor is called. Every class has a constructor. When a constructor is not defined in a class, the system will assign a constructor with an empty parameter to the class. This is the default constructor in the class. If the constructor is customized in the class, then the default constructor is gone.

Constructor overloading and method overloading in Java are very similar. You can create multiple constructors for a class. Each constructor must have its own unique parameter list.

Java does not support copy construction methods like in C++. This difference is because if you do not write the construction method yourself, Java will not create a default copy construction method.

The method name of the constructor must be the same as the class name

The constructor has no return type and cannot be defined as void. The method type is not declared before the method name.

The main function of the construction method is to complete the initialization of the object. It can pass the parameters when defining the object to the domain of the object.

A class can define multiple constructors. If no constructor is defined when the class is defined, the compiler system will automatically insert a default constructor with no parameters. This constructor does not execute any code.

The construction method can be overloaded, with the number, type, and order of the parameters.

The construction method is a special method.
Method structure:
access modifier class name (parameter list) {method body}

The constructor is overloaded.
Without writing the construction method, there is a default
class name () {} construction method

this() is to call the construction method of this class without parameters;
super() is to call the construction method of the parent class without parameters;

The similarity between the construction method and the method is that they both contain executable code. The difference is that the construction method is executed only when the Java class is instantiated. The constructor usually contains the initialization code of the class attribute. Strictly speaking, the constructor is not a method, because it does not return any value.
————————————————
Copyright Statement: This article is the original article of the CSDN blogger "HSXaaa123", and it follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting. .
Original link: https://blog.csdn.net/weixin_41924879/article/details/100672169 occurred in inheritance

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/105041278