Question 6
Consider the Java code given below.
import java.util.*;class Book implements Cloneable { String title; String author; public Book(String t, String a) { this.title = t; this.author = a; } public Book clone() throws CloneNotSupportedException { return (Book) super.clone(); } public String toString() { return title + ":" + author; }}class Library implements Cloneable { String libraryName; List<Book> books; public Library(String l, List<Book> b) { this.libraryName = l; this.books = b; }
public Library clone() throws CloneNotSupportedException { Library clonedLibrary = (Library) super.clone(); clonedLibrary.books = new ArrayList<>(); for (Book book : this.books) { clonedLibrary.books.add(book.clone()); } return clonedLibrary; }}public class TestCloning { public static void main(String[] args) throws CloneNotSupportedException { List<Book> books = new ArrayList<>(); books.add(new Book("1984", "George Orwell")); books.add(new Book("Outline", "Rachel Cusk")); Library library1 = new Library("Central Library", books); Library library2 = library1.clone(); library2.books.get(0).title = "Hanging"; System.out.println(library1.books); System.out.println(library2.books); }
}What will the output be?