-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.java
More file actions
35 lines (32 loc) · 906 Bytes
/
ex2.java
File metadata and controls
35 lines (32 loc) · 906 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
30
31
32
33
34
35
public class ex2 {
int id;
String name;
String city;
ex2(){
System.out.println("default constructor invoked");
}
ex2(int id,String name){
this();//it is used to invoked current class constructor.
this.id = id;
this.name = name;
}
ex2(int id,String name,String city){
// constructor chaining
//Call to this() must be the first statement in constructor.
this(id,name);//now no need to initialize id and name
this.city=city;
}
void display(){System.out.println(id+" "+name+" "+city);}
public static void main(String args[]){
ex2 e1 = new ex2(111,"karan");
ex2 e2 = new ex2(222,"Aryan","delhi");
e1.display();
e2.display();
}
}
/*output
*default constructor invoked
default constructor invoked
111 karan null
222 Aryan delhi
*/