-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloneExample.java
More file actions
80 lines (68 loc) · 2.65 KB
/
CloneExample.java
File metadata and controls
80 lines (68 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Address {
Location location;
String state;
String country = "India";
public Address(Location location, String state) {
this.location = new Location(location.street, location.city);
this.state = state;
}
@Override
public String toString() {
return "\nCountry = " + country + "\nState = " + state +"\nLocation:" + location + "\n";
}
}
class Location {
String street;
String city;
public Location(String street, String city) {
this.street = street;
this.city = city;
}
@Override
public String toString() {
return "\nCity = " + city + "\nStreet = " + street;
}
}
class UPIApps implements Cloneable {
String name;
int dailyLimit;
double maxDailyAmount;
Address headOfficeLocation;
public UPIApps(String name, int dailyLimit, double maxDailyAmount, Address headOfficeLocation) {
this.name = name;
this.dailyLimit = dailyLimit;
this.maxDailyAmount = maxDailyAmount;
this.headOfficeLocation = headOfficeLocation;
}
@Override
protected Object clone() throws CloneNotSupportedException {
//Location loc = new Location(this.headOfficeLocation.location.street, this.headOfficeLocation.location.city);
Address newDepObj = new Address(this.headOfficeLocation.location, this.headOfficeLocation.state);
return new UPIApps(this.name,this.dailyLimit,this.maxDailyAmount,newDepObj);
}
@Override
public String toString() {
return "\n---- Name = " + name +" ----\nDaily Limit = " + dailyLimit + "\nMax Daily Amount = " + maxDailyAmount +"\n-- Head Office Location: --"
+ headOfficeLocation + "\n";
}
}
public class CloneExample {
public static void main(String[] args) throws Exception {
Location BHIMloc = new Location("123 park road","Banglore");
Address BHIMadrs = new Address(BHIMloc,"Karnataka") ;
UPIApps bhim = new UPIApps("BHIM", 10, 100000,BHIMadrs);
// Before cloning
System.out.println("----------- Before Deep cloning: -----------");
System.out.println("BHIM app info:\n"+bhim);
// Deep copy
UPIApps payTm = (UPIApps) bhim.clone();
// Changing Data
payTm.name = "PayTm";
payTm.headOfficeLocation.location.city = "Mumbai";
payTm.headOfficeLocation.location.street = "456 RPTS Road";
payTm.headOfficeLocation.state = "Maharashtra";
System.out.println("----------- After Cloning -----------\nChanges in paytm\n" + payTm);
//Printing BHIM app info
System.out.println("BHIM app info after Cloning:\n"+bhim);
}
}