Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.
";
    }
    // 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 + "
";
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
254 views
Welcome To Ask or Share your Answers For Others

1 Answer

(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 errorNo 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...