简体   繁体   中英

What is the purpose of a no-arg constructor?

I understand most of the code below. I just don't understand the purpose of the no-arg constructors of Employee and Person since I get the same results when I delete them. I am new to java so I apologize if this is a dumb question. This is a code snippet for one of my classes. Any help will be appreciated.

public class TestEmployee {
    public static void main(String[] args) {        
        // Define some employees
        Employee president  = new Employee("Lucy", "President", 100000);        
        System.out.println(president);  


        Employee cto  = new Employee("Vincent", "Chief Tech Officer", 70000);       
        System.out.println(cto);    
    }
}

class Employee extends Person{
    // ADD YOUR CODE HERE!!!
    // Nothing above needs to change.
    private String jobTitle="Unknown";
    private int salary = 0;

    public Employee() {

    }

    public Employee(String name, String jobTitle, int salary) {
        super(name);
        this.jobTitle = jobTitle;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return this.getName() + " is the " + jobTitle + " and makes $" + salary + " a year.\n";
    }
    // You need data fields, a constructor or two, and a to-string method
}

// IF YOU ALREADY HAVE THE PERSON CLASS IN YOUR WORKSPACE
// YOU CAN DELETE THIS. OTHERWISE YOU'LL GET AN ERROR:
// 'The type Person is already defined.'
class Person {
    private String name = "Default Name";

    // No-arg Constructor
    public Person () {
    }

    public Person (String name){
        this.name = name;
    }

    // Getter method
    public String getName() {
        return name;
    }

    // Setter method
    public void setName(String newName) {
        if (!newName.equals("")) {
            name = newName;
        }
        else
            System.out.println("Can't change name. Empty names aren't allowed!");
    }

    public String toString() {
        return "Name: " + name + "\n";
    }
}

(a)The no-arg constructor you are referring to is a explicitly defined substitute to the "default constructor".If the programmer doesn't explicitly define a constructor,then the compiler(javac) automatically defines a default constructor as ClassName(){} .This constructor is not visible in the code,however after the compilation is done,it is present in the java bytecode.

(b)In your case, there is a explicitly defined no-arg constructor.The reason being that you have explicitly defined a parameterised constructor public Employee(String name, String jobTitle, int salary) public Person (String name) If parameterised constructors are defined explicitly in a program the Java compiler doesn't insert the implicit default constructor.So if you wish to make the object of the class without passing any initial values Employee president = new Employee(); This statement would throw an error No default constructor found; nested exception is java.lang.NoSuchMethodException: No default constructor found; nested exception is java.lang.NoSuchMethodException: .Thus there are explicit definations for the no-arg constructors to support such object creation,where a user may allocate heap space for a object first,and then initialise it later using the setter methods or some other mechanisms.

It is superfluous in this case.

However a private argument-less, empty (or not) constructor serves a purpose from an interface design standpoint, to prevent instantiation and force users of the class to use more expressive static factory methods, which can be considered good design. I believe this point is made in item 1 of Joshua Bloch's 'Effective Java'.

A constructor with or without parameters has a purpose of creating in object in order to access the methods in its class. With the no parameter constructor, you are able to create the object in order to access the methods in its class. Also, since it extends another class, any methods in that other class can also be accessed when you created a new object through that blank constructor.

In Java, a no-argument constructor is the default constructor and if you don't define explicitly in your program. Then Java Compiler will create a default constructor with no arguments.The purpose is to call the superclass constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM