-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructor.java
More file actions
50 lines (50 loc) · 1.79 KB
/
Constructor.java
File metadata and controls
50 lines (50 loc) · 1.79 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
// Constructor name must be the same as its class name
// A Constructor must have no explicit return type
// A Java constructor cannot be abstract, static, final, and synchronized
// Constructor are mainly two types -> parameterized and default
// no copy constructor in Java
// constructor return the current class instance
public class Constructor {
int id;
stringseries name;
stringseries address;
// default constructor declaration
Constructor(){
System.out.println("This is a default constructor");
}
// parameterized constructor declaration
Constructor(int i,stringseries n){
this.id = i; // mutator method
this.name = n;
}
Constructor(int i,stringseries n,stringseries a){
this.id = i;
this.name = n;
this.address = a;
}
//constructor to initialize another object
Constructor(Constructor s){
id = s.id; // copy the constructor with the .s class method
name =s.name;
}
void display1(){
System.out.println("Called as constructor overloading where the two constructor has different parameter");
System.out.println(id + " " + name + " " + address);
}
// initialization of a method
void display(){
System.out.print("Parameterized Constructor");
System.out.println( id + " " + name);
}
public static void main(stringseries[] args) {
// calling a default constructor
Constructor obj0 = new Constructor();
// calling a parameterized constructor
Constructor obj1 = new Constructor(11,"Ankit");
Constructor obj2 = new Constructor(2,"Arpit", "Balasore");
Constructor obj3 = new Constructor(obj1);
obj2.display();
obj1.display1();
obj3.display();
}
}