I looked at this link, but didn't really understand what was meant by the clone
Anyways onto my question, so I have a class which inherits two attributes from another class, the class it inherits from has set/get methods and a copy constructor.
I want to implement a copy constructor into the inheriting class (ignore comments in code)
public class Instructor extends Person{
//officeNumber represents the office number where instructors can be found
private String officeNumber;
//constructor allows user to define first and last name and office number in demo
public Instructor(String fName, String lName, String officeNumber) {
super(fName, lName);
this.officeNumber=officeNumber;
}
}
I want to put the copy constructor here and so far all I can do is this, but I can't just pass in an object into another constructor in a demo class, I have to include the full name
public Instructor(String fName, String lName,Instructor object2) {
super(fName,lName);
officeNumber=object2.officeNumber;
}
//get method for field
public String getOfficeNumber() {
return officeNumber;
}
}
Here is the class it inherits from if that's any help
public class Person {
//firstName represents the first name of a person
private String firstName;
//lastName represents the last name of a person
private String lastName;
//constructor allows programmer to define first and last name of object in demo
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//copy constructor
public Person(Person object2) {
firstName = object2.firstName;
lastName = object2.lastName;
}
//get methods for fields
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
firstNameandlastName. I would highly suggest starting with a good beginner's book on Java or the Oracle Java tutorials.