I'm trying to get a cloneable interface to work. Here's what I have so far:
Driver.java
package interfaces.business; import java.util.Scanner; import java.time.*; public class Driver implements Cloneable{ private int DriverID; private String firstName; private String lastName; public Driver(int DriverID, String firstName, String lastName) { this.DriverID = DriverID; this.firstName = firstName; this.lastName = lastName; } public void setDriverID(int DriverID) { this.DriverID = DriverID; } public int getDriverID() { return DriverID; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getFirstName() { return firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getLastName() { return lastName; } //To make the clone() method available to all classes (instead of just having protected access), //you can override the clone() method of the Object class with a clone() method that has public //access: @Override public Object clone() throws CloneNotSupportedException{ return super.clone(); } }
CloneInterface.java
package interfaces.ui; import interfaces.business.Driver; public class CloneInterface implements Cloneable{ public static void main(String[] args) { try { System.out.println("The main method from the CloneInterface class is being used."); System.out.println(); Driver z = new Driver(5580,"Sam","Peterson"); z.setDriverID(5580); z.setFirstName("Sam"); z.setLastName("Peterson"); //clone the object Driver z2 = (Driver) z.clone(); //change a value of the cloned object z2.setDriverID(8888); //print the results System.out.println(z); System.out.println(z2); }catch (CloneNotSupportedException ex) { System.out.println(ex); } } }
I was expecting the output to look something like this:
5580, Sam, Peterson
8888, Sam, Peterson
But instead, I got something like this:
interfaces.business.Driver@15db9742
interfaces.business.Driver@6d06d69c
What happened, and how do I fix it?