greekman
greekman

Reputation: 91

How to deep copy an object from another class?

I have another class Author that returns the first and last name of an author and their email address. I need to do a deep copy of a book but it won't let me copy the author.

I thought I could just do use my getAuthor method but it isn't working.

Any help would be greatly appreciated. Thank you!!

    private String isbn;
    private String title;
    private String publisher;
    private Object Author;
    private int pages;

    public Book(String isbn, String title, Author author, String publisher, int pages) {
        this.isbn = isbn;
        this.title = title;
        this.publisher = publisher;
        this.pages = pages;
        this.Author = author;
    }

    public void setNumPages(int pageNum){
        pages = pageNum;
    }

    public String getTitle() {
        return title;
    }

    public Object getAuthor() {
        return Author;
    }

    public String getIsbn(){
        return isbn;
    }

    public String getPublisher() {
        return publisher;
    }

    public String toString(){
        return (title + ", " + getAuthor() + " (ISBN-10 #" + isbn + ", " + pages + " pages)");
    }

    public Book (Book other) {
        this(other.isbn, other.title, other.getAuthor(), other.publisher, other.pages);
    }

    public boolean equals(Object obj) {
        Book book = (Book) obj;

        if (this.isbn.equals(book.getIsbn()) && this.title.equals(book.getTitle())
                && this.getAuthor().equals(book.getAuthor())) {
            return true;
        } else {
            return false;
        }

    }
}

Upvotes: 0

Views: 88

Answers (1)

DelfikPro
DelfikPro

Reputation: 729

Your Author field has the Object type:

private Object Author;

You need to change Object to Author.

Also, consider renaming the field from Author to author because it may be confused with the Author class.

Upvotes: 2

Related Questions