Created
March 23, 2021 12:41
-
-
Save NAVNEETOJHA/0bb7ac54ca07cc957197e5d91b9065e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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