-4

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;
    }
}
8
  • 1
    It would appear you don't understand variable scope, objects, inheritance, or how to use methods. Your base class gives you getters for both firstName and lastName. I would highly suggest starting with a good beginner's book on Java or the Oracle Java tutorials. Commented Jul 19, 2013 at 4:08
  • If anything is unclear, just send me a comment and I'll try to fix. Commented Jul 19, 2013 at 4:09
  • Yes I know, I can't say fName=object2.fName because they're not from the same method, but previously I did not use inheritance and had a copy constructor for this class and would like to reinstate that while keeping the whole inheritance thing going Commented Jul 19, 2013 at 4:10
  • Well, no one has yet to answer my question or even explain what I'm doing wrong. So besides editing the thanks out of my original post and adding a few spaces, no one has done anything constructive. Commented Jul 19, 2013 at 4:12
  • People should understand that not every question on this site is going to be about high level programming, some of us are rudimentary programmers who don't know where to find answers. Commented Jul 19, 2013 at 4:20

1 Answer 1

1

Please elaborate your question.

You could have a copy constructor in Instructor like this,

This code is only for understanding.

public class Instructor extends Person {
    public String getIname() {
        return iname;
    }

    public void setIname(String iname) {
        this.iname = iname;
    }

    private String iname;

    public Instructor(Person p, String myinstructor) {
        super(p);
        this.iname = myinstructor;
    }

    public Instructor(Instructor clone) {
        super(clone);
        this.iname = clone.iname;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

What is wrong with the above. Why is it down voted?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.