Home > Net >  Multiple Constructors without changing previous data
Multiple Constructors without changing previous data

Time:07-09

I am in an oop class that is being presented with java. I understand the concept of instantiating an object and using multiple constructors with overloading. In my current assignment, however, I am required to have at least two constructors but my objects all require the same data. They are books, each with a name, title, and isbn. all String.

I can't overload the constructor if I have the same signature. I also can not chain the constructors because I am already calling the super constructor in the first line.

If I separate something, such as the name, then call one constructor for the title and isbn then a second constructor for the name of course I have constructed a new object, or overwritten the old one in this case.

I am at a loss of what to do, I was trying to search if there was a way to call a second constructor for the same object to send the missing pieces of information. but came up empty-handed. I could use a setter method, but that does not satisfy the multiple constructors requirements.

Thanks for your help.

CodePudding user response:

I am required to have at least two constructors but my objects all require the same data. They are books, each with a name, title, and isbn. all String.

Here is a starting point that provides a class with a single constructor, and members for name, title and isbn.

class Book {
    String name;
    String title;
    String isbn;

    public Book(String name, String title, String isbn) {
        this.name = name;
        this.title = title;
        this.isbn = isbn;
    }
}

If I separate something, such as the name, then call one constructor for the title and isbn then a second constructor for the name of course I have constructed a new object, or overwritten the old one in this case.

I like the idea of separating out a field, such as name, like this:

public Book(String title, String isbn) {
    this.title = title;
    this.isbn = isbn;
}

And then after you called that constructor, you could set the "name" value:

Book book1 = new Book("title1", "isbn1");
book1.name = "name1";

UPDATE: I missed this, that you cannot use a setter: "I could use a setter method, but that does not satisfy the multiple constructors requirements."

Note that it's better to make name, title, and isbn all "private", and expose a getter to modify the value:

public void setName(String name) {
    this.name = name;
}

Another option for a second constructor could be to set a value for "name" with some placeholder ("NOT SET"):

public Book(String title, String isbn) {
    this.name = "NOT SET";
    this.title = title;
    this.isbn = isbn;
}

Or rewrite that second constructor to call the first one:

public Book(String title, String isbn) {
    this("NOT SET", title, isbn);
}

Here's what it would look like if you had three constructors:

  • one that takes "title" only (assume that's the bare minimum of what is known about a book),
  • one that takes title name (where "name" means the author?"), and
  • one that takes all three: title, name and isbn.
public Book(String title) {
    this(title, "unknown", "unknown");
}

public Book(String title, String name) {
    this(title, name, "unknown");
}

public Book(String title, String name, String isbn) {
    this.name = name;
    this.title = title;
    this.isbn = isbn;
}

If either of the first two constructors is called, a default value "unknown" is used.

CodePudding user response:

maybe a private and a public constructor? but i feel like there is something missing, if u can share the exact question?

  • Related