We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8b80023 commit 35b661bCopy full SHA for 35b661b
Constructors/CopyConstructor.java
@@ -0,0 +1,31 @@
1
+class Person {
2
+ String name;
3
+ int age;
4
+
5
+ Person(String name, int age) {
6
+ System.out.println("This is the original constructor");
7
+ this.name = name;
8
+ this.age = age;
9
+ }
10
11
+ Person(Person p)
12
+ {
13
+ System.out.println("This is a copy of the constructor");
14
+ name=p.name;
15
+ age=p.age;
16
17
18
+ // Overriding the toString of Object class
19
+ public String toString() {
20
+ return "Name:" + name + "\tAge:" + age;
21
22
23
+}
24
+class CopyConstructor {
25
+ public static void main(String[] args) {
26
+ Person p1=new Person("Disha",22);// original
27
+ System.out.println(p1);
28
+ Person p2=new Person(p1);//copy
29
+ System.out.println(p2);
30
31
0 commit comments