-6

Beginner here.

I cant get my head around why this code outputs the default halfway through. Can anyone take look?

sorry if the format is wrong, first time posting and will fix if not correct.

 public class officemanager {
    public static void main(String[] args) {

        Staffmember aStaffMember = new Staffmember("Steven", "bob");
        System.out.println(aStaffMember.toString());

        Programmer appleprg = new Programmer("Marion", "bob", "Java");
        appleprg.getLanguage();
        System.out.println(appleprg.toString());

        Doctor dr = new Doctor();
        dr.setWard(5);
        dr.setFirstName("ed");
        dr.setLastName("fall");

        System.out.println(dr.toString());

    }
}

OUTPUT

Staffmember firstName=Steven, lastName=bob
Programmer firstName=Marion , lastName=bob   language Java
default constructor
Doctor firstName=ed , lastName=fall      Ward 5

Sorry guys here the class the default constructor is in. It is the Superclass called Staffmember and the firstname, lastname Strings are passed through it.

package oopinheritance;

public class Staffmember {

private String firstName;
private String lastName;


// default constructor
public Staffmember() {
System.out.println("default constructor");
    }

// constructor
public Staffmember(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;

}

public String getFirstName() {
return firstName;}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;}

public void setLastName(String lastName) {
this.lastName = lastName;}



public String toString() {
return "Staffmember firstName="
+firstName+ ", lastName=" + lastName;
}
}

Here is the Doctor class, it is a subclass of Staffmember and it has its own tostring method:

package oopinheritance;

public class Doctor extends Staffmember{
private int ward;



public int getWard() {
return ward;
}



public void setWard(int ward) {
this.ward = ward;
}



public String toString() {
return "Doctor firstName="
+this.getFirstName() + " , lastName=" + this.getLastName() + " \t          
ward"     +    this.ward;
}



}
Bangorsteve
  • 19
  • 1
  • 6
  • You haven't shown the code that prints "default constructor" so it's hard to say why it's called and when... – assylias Sep 05 '17 at 11:59
  • 2
    Impossible to tell with your current code, but likely the default constructor in the `Doctor` class outputs `default constructor` when invoked. – Mena Sep 05 '17 at 11:59
  • 1
    Because `new Doctor()` calls the no-arg constructor? – Andrew Li Sep 05 '17 at 11:59
  • Doctor dr = new Doctor(); is calling the default constructor since you give no parameters – Martin Chekurov Sep 05 '17 at 12:01
  • Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Sep 05 '17 at 12:04
  • OK thanks guys. I have added the Staffmenber and Doctor Classes so you can see whats going on behind that. Im trying to do Doctor a different way without a method. – Bangorsteve Sep 05 '17 at 12:51

1 Answers1

0

As you have not shown your whole program, so its hard to tell where is the error, but it might be in the default constructor of the doctor class.

Anyways here is the code that you can refer. It will give the correct output.

Here is the link you can refer to see the execution order http://javabeginnerstutorial.com/learn-by-example-3/order-of-execution-of-blocks-in-java/

Java Constructors - Order of execution in an inheritance hierarchy

class GfG {
    public static void main(String[] args) {

        Staffmember aStaffMember = new Staffmember("Steven", "bob");
        System.out.println(aStaffMember.toString());

        Programmer appleprg = new Programmer("Marion", "bob", "Java");
        appleprg.getLanguage();
        System.out.println(appleprg.toString());

        Doctor dr = new Doctor();
        dr.setWard(5);
        dr.setFirstName("ed");
        dr.setLastName("fall");

        System.out.println(dr.toString());

    }
}

class Staffmember {
    String firstName;
    String lastname;

    public Staffmember(String firstName, String lastname) {
        super();
        this.firstName = firstName;
        this.lastname = lastname;
    }

    @Override
    public String toString() {
        return "Staff Member firstName=" + firstName + ", lastname=" + lastname;
    }

}

class Programmer {
    String firstName;
    String lastName;
    String Language;

    public String getLanguage() {
        return Language;
    }

    public void setLanguage(String language) {
        Language = language;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Programmer(String firstName, String lastname, String Language) {
        super();
        this.firstName = firstName;
        this.lastName = lastname;
        this.Language = Language;
    }

    @Override
    public String toString() {
        return "Programmer firstName=" + firstName + ", lastName=" + lastName + ", Language=" + Language;
    }

}

class Doctor {
    int ward;
    String firstName;
    String lastName;

    public void setWard(int ward) {
        this.ward = ward;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastname) {
        this.lastName = lastname;
    }

    public Doctor(int ward, String firstName, String lastName) {
        super();
        this.ward = ward;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Doctor ward=" + ward + ", firstName=" + firstName + ", lastName=" + lastName;
    }

}
Abhishek Honey
  • 645
  • 4
  • 13
  • Thanks for this. I'm trying to pull Doctor through the Staffmember Superclass and add "ward" onto that. So I'm trying to do it without creating a Doctor constructor. I've now added the other classes so u can see. – Bangorsteve Sep 05 '17 at 12:53
  • This is the line that is printing the default constructor ****// default constructor public Staffmember() { System.out.println("default constructor"); }****. As you are making the object of the child class so the parent class default constructor gets initialized first and then the child class constructor gets initialized. So even if you are making the constructor of the doctor class the parent class ie StaffMember counstructor gets called. Just see this link you will understand https://stackoverflow.com/questions/19407187/java-constructors-order-of-execution-in-an-inheritance-hierarchy – Abhishek Honey Sep 05 '17 at 13:18
  • OK I can see the Doctor is calling the default constructor for Staffmember. It seems to call that first and then do the correct thing in the next line? :( :( – Bangorsteve Sep 05 '17 at 13:34
  • Yes the flow of java is like that only, the parent class constructor is called before the child class constructor. You can see it like this, a child can never exists without parent in real life, so the parent class constructor is always called first. – Abhishek Honey Sep 05 '17 at 13:37
  • I see .. . but how come none of my other classes call the default constructor like Doctor does and makes a mess of my output?! – Bangorsteve Sep 05 '17 at 13:41
  • I think its because I put parameters in their instantiations. Thanks for your help. :) We got there in the end. So the programme is doing nothing wrong then? – Bangorsteve Sep 05 '17 at 13:54
  • Yes it is doing nothing wrong. – Abhishek Honey Sep 05 '17 at 14:03