Skip to content

Instantly share code, notes, and snippets.

@NAVNEETOJHA
Created March 23, 2021 12:41
Show Gist options
  • Save NAVNEETOJHA/0bb7ac54ca07cc957197e5d91b9065e6 to your computer and use it in GitHub Desktop.
Save NAVNEETOJHA/0bb7ac54ca07cc957197e5d91b9065e6 to your computer and use it in GitHub Desktop.
class Address
{
public String streetAddress, city, country;
public Address(String streetAddress, String city, String country)
{
this.streetAddress = streetAddress;
this.city = city;
this.country = country;
}
public Address(Address other)
{
this(other.streetAddress, other.city, other.country);
}
@Override
public String toString()
{
return "Address{" +
"streetAddress='" + streetAddress + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
'}';
}
}
class Employee
{
public String name;
public Address address;
public Employee(String name, Address address)
{
this.name = name;
this.address = address;
}
public Employee(Employee other)
{
name = other.name;
address = new Address(other.address);
}
@Override
public String toString()
{
return "Employee{" +
"name='" + name + '\'' +
", address=" + address +
'}';
}
}
class CopyConstructorDemo
{
public static void main(String[] args)
{
Employee john = new Employee("John",
new Address("123 London Road", "London", "UK"));
//Employee chris = john;
Employee chris = new Employee(john);
chris.name = "Chris";
System.out.println(john);
System.out.println(chris);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment