-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26_Constructor.java
More file actions
182 lines (141 loc) · 4.03 KB
/
26_Constructor.java
File metadata and controls
182 lines (141 loc) · 4.03 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.*;
class Solution {
int x;
// Constructors are used to initialise the object.
Solution(){
System.out.println("Inside constructor 0");
x = 21;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Solution obj1 = new Solution();
System.out.println(obj1.x);
}
}
class Solution1 {
int x;
// Constructor name must match with the class name
// Constructor cannot return any data.
// Parameterised constructor
Solution(int x, int y){
System.out.println("Inside constructor");
this.x = x+y;
// the data member is refrenced using this keyword.
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Solution obj1 = new Solution(3, 2);
System.out.println(obj1.x);
}
}
class Solution2 {
int x, y;
// Constructor overloading.
Solution(){
System.out.println("Constructor 0");
x = y = 0;
}
Solution(int x){
System.out.println("Constructor 1");
this.x = x;
// Default value will be zero.
this.y = 0;
}
Solution(int x, int y){
System.out.println("Constructor 2");
this.x = x;
this.y = y;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Solution obj1 = new Solution();
Solution obj2 = new Solution(21);
Solution obj3 = new Solution(21, 15);
System.out.println(obj1.x + " " + obj1.y);
System.out.println(obj2.x + " " + obj2.y);
System.out.println(obj3.x + " " + obj3.y);
}
}
class Solution3 {
// Experiments on constructors access specifier
// All the three access specifiers are allowed.
// 1. public
// 2. private
// 3. protected
// What are these specifiers.
// What is the default access specifiers.
// public access
public Solution(){
System.out.println("Constructor0");
}
// private access
private Solution(int x){
System.out.println("Constructor1");
}
// protected access
protected Solution(int x, int y){
System.out.println("Constructor2");
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Solution obj1 = new Solution();
Solution obj2 = new Solution(3);
Solution obj3 = new Solution(3, 2);
}
}
class Solution4 {
static int x, y; // Default value of data members: 0
// Experiment with variable declaration.
Solution(){
System.out.println("Constructor0");
// Create a non-static variable: Allowed
int temp;
// swap
temp = x;
x = y;
y = temp;
System.out.println(x + y);
}
Solution(int x){
System.out.println("Constructor1");
x += x;
y += y;
// Static declaration of members are not allowed.
// static int z = 13;
}
Solution(int x, int y) {
System.out.println("Constructor2");
// Initialise the static variables.
this.x = x;
this.y = y;
}
void display(){
System.out.println(x + " " + y);
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// Initialise variable x and y
Solution obj1 = new Solution(21, 15);
obj1.display();
// Swap x and y
Solution obj2 = new Solution();
obj1.display();
// Increase the x and y
Solution obj3 = new Solution(1);
obj1.display();
}
}
class Solution5 {
// Exp: Static keyword with constructor
// Error: Static modifier not allowed with constructors.
// static Solution(){
// System.out.println("Constructor0");
// }
Solution(){
System.out.println("Constructor 0");
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Solution obj1 = new Solution();
}
}