Home  >  Article  >  Computer Tutorials  >  Why does the Object class require a null-parameter constructor in Java?

Why does the Object class require a null-parameter constructor in Java?

PHPz
PHPzforward
2024-01-24 18:39:04723browse

java中为什么 Object类要有一个空的构造函数

Why does the Object class have an empty constructor in java

Other classes do not need to explicitly define parameterless constructors because they inherit the Object class and thus inherit the Object constructor method.

Object is the parent class of all classes. It has no other classes inheriting it, so it must have a no-argument constructor for instantiation.

Java is a cross-platform object-oriented programming language with excellent versatility, efficiency, platform portability and security. It is widely used in PCs, data centers, game consoles, scientific supercomputers, mobile phones and the Internet. At the same time, Java has the largest professional community of developers in the world, providing a solid foundation for its further development and innovation.

Object object is often used to imitate some objects around us in the real world.

A software object is a model of a real-world object that has state and behavior. State is maintained through variables, which are named by user identifiers. Methods are functions associated with an object that perform the object's behavior. Therefore, a software object is a model that maintains state through variables and performs behavior through methods.

Objects are the key to understanding object-oriented technology. Before learning, you can observe real-life objects such as dogs, tables, TVs, bicycles, etc. These objects have two characteristics in common: state and behavior. For example, dogs have their own states (such as name, color, fertility, hunger, etc.) and behaviors (such as wagging tail). Likewise, a bicycle has state (such as current gear, wheels, etc.) and behavior (such as braking, accelerating, decelerating, and changing gears). By observing these objects, we can better understand object-oriented technology.

What is the significance of writing empty C constructor function body

Object instantiation and data initialization are usually completed through constructors. In Java, all classes inherit from the Object class, so the empty parameter constructor of the Object class is inherited by default. This means that even if you don't write a null constructor, it still exists. However, parameterized constructors are generally written by developers themselves. The parameterized constructor works similarly to the empty-parameter constructor, but it can receive parameters and perform some specific initialization operations. Therefore, parameterized constructors allow more flexibility in initializing an object's data. In summary, the empty parameter constructor is the default constructor inherited from the Object class, while the parameterized constructor is written by the developer himself and can receive parameters and perform specific initialization operations.

Let me give you an example to illustrate

There is a class Monitor which has the attribute String height;String

width

public Monitor(){}

public Monitor(String height,String width){}

A constructor method with one empty parameter and one parameter

In the main method I do the initialization action

Monitor monitor=new Monitor();

This structure only creates the display object, and its properties have no value. You can also manually assign values ​​to it

For example: monitor.height=100;

monitor.width=200;

It’s different if you use one with parameters, there are a lot less things to do

Monitor monitor2=new Monitor("100","200");

Here I only need to put the parameters I want to put in directly to assign values ​​to my attributes. Does this save a lot of work?

This is its advantage!

Answer completed!

The constructor is a special method that is mainly used to initialize the object when creating it, that is, assign initial values ​​to the object member variables

Always used with the new operator in the statement of creating an object. A special class can have multiple constructors, which can be distinguished according to the number of parameters or the type of parameters, that is, the overloading of the constructor

The difference between constructor and other methods

1. The name of the constructor must be exactly the same as the class name; while the general method cannot be the same as the class name.

2. The function of the constructor is mainly used to define the initialization state when the object of the class is created. It has no return value and cannot be modified with void. This ensures that not only does it automatically return nothing, but it cannot return anything at all. There is no choice. And other methods have return values. Even if it is a void return value, although the method body itself will not automatically return anything, you can still make it return something, and these things may be unsafe.

3. The constructor cannot be called directly. It must be automatically called when the object is created through the new operator. The general method is called when the program is executed.

4. When defining a class, the constructor of the class will usually be displayed, and the initialization work can be omitted in the function. The Java compiler will provide a default constructor. This default constructor Functions take no parameters. This feature does not exist in general methods

Simply speaking, the constructor is used to create objects. More professionally, it is called instantiation of objects.

definition:

class A

{

public A(){}

}

use:

A a=new A();//Here, A() is the constructor.

One thing to note is that if there is no constructor defined in the class, the compiler will automatically generate an empty constructor, and each member of the class will be assigned a default value.

But if only one private constructor is defined in the class, then this class will not be instantiated.

class A

{

string a;

private A() { }//This class cannot be instantiated

}

If we define a constructor with parameters but not a constructor without parameters, parameters must be provided when instantiating an object of this class.

class A

{

string a;

public A(string str) {a=str; }

}

You cannot instantiate objects like this A a=new A();

Constructors are divided into dynamic constructors and static constructors.

The dynamic constructor allocates memory space for a class or structure when it is instantiated, and completes the initialization of member variables in the class or structure. Dynamic constructors can be overloaded, that is, the same constructor name has multiple constructors of different types and numbers. There is no essential difference between the constructor with parameters and the constructor without parameters. The constructor with parameters can be controlled by the passed variables or directly initialize the member variables. In the constructor, the default value can be initialized for the member variables.

Static constructors are not allowed to carry parameters, that is, only one static constructor is allowed in a class. It is called before the first access to a static member or the first instantiation of a dynamic object of this class.

c constructor generates empty string

#include

#include

using namespace std;

#define MAXLENGTH 1000

class CString

{

char m_buff[MAXLENGTH];

public:

CString(); //Constructor 1, set to empty string

~CString();

friend ostream& operator

char operator[](int i)const;

};

CString::CString()

{

//m_buff[0]=NULL; What do you mean?

memset(m_buff,0,sizeof m_buff); //String initialization function

}

char CString::operator[](int i)const

{

return m_buff[i];

}

ostream& operator

{

return Out

}

CString::~CString()

{

//delete[] m_buff; Brother, you cannot use delete without new. Delete must be dynamically allocated.

}

int main()//Generally write int

{

CString s1;

cout

return 0; //You don’t need to write

} Anyway, no error is reported. As for whether the function you want can be realized, that is your business. c The road is long, so walk slowly!

What is a constructor method in java

The construction method is a special method, which is different from the general method:

1. The name of the constructor must be exactly the same as the class name in which it is defined. There is no return type, not even void.

2. The constructor method is called using the new operation when creating an object. The function of the constructor is to initialize the object.

3. Cannot be modified by static, final, synchronized, abstract and native. Constructors cannot be inherited by subclasses.

class RectConstructor{

double length;

double width;

double area(){

return length*width;

}

//The following method is the so-called construction method. It has no return value and the method name is the same as the class name~~~

RectConstructor(double width,double length){//Constructor method with parameters

this.length=length;

this.width=width;

}

}

In fact, the construction method is to assign initial values ​​to the members of the class~~~~~~~~~~~~~

The above is the detailed content of Why does the Object class require a null-parameter constructor in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete