Skip to content

Commit 35b661b

Browse files
authored
Add files via upload
1 parent 8b80023 commit 35b661b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: Constructors/CopyConstructor.java

+31
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)