-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructor_Chaining.java
More file actions
29 lines (25 loc) · 914 Bytes
/
Copy pathConstructor_Chaining.java
File metadata and controls
29 lines (25 loc) · 914 Bytes
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
package Miscellaneous;
/*
Constructor chaining within a same class can be done with overloaded constructors. And chaining of constructors can be done using this() keyword.
Here object instance will call the parameterized constructor --> TestFile(String name, int age)
first line, this(name) will call the constructor --> TestFile(String name)
So the chaining will end at default constructor
Note: constructor declaration doesn't matter, you can declare constructor in any order.
*/
class TestFile{
TestFile()
{
System.out.println("Default Constructor");
}
TestFile(String name){
this();
System.out.println("Name:" + name);
}
TestFile(String name, int age){
this(name);
System.out.println("Name:" + name + " Age:" + age);
}
public static void main(String[] args) {
TestFile t = new TestFile("Kanav",15);
}
}