-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh09_41_Constructors.java
More file actions
66 lines (54 loc) · 1.94 KB
/
Ch09_41_Constructors.java
File metadata and controls
66 lines (54 loc) · 1.94 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
class studentClass{
private int roll_no;
private String name;
public studentClass(String username){
roll_no = 43;
name = username;
}
// Setter
public void setName(String n) {
name = n;
}
public void setRoll_no(int rn) {
roll_no = rn;
}
// Getter
public String getName(){
return name;
}
public int getRoll_no(){
return roll_no;
}
}
public class Ch09_41_Constructors {
public static void main(String[] args) {
System.out.println();
// Constructors helps in initialising an object while it is created.
// That is no need to give values to attributes of object again and again.
/* Constructor Syntax:
access_modifier Class_name(){
attribute = default value you want to give
}
*/ // That is if attributes are not given a new value , default values will be considered.
// studentClass ved = new studentClass();
// System.out.println("Default Name: " + ved.getName());
// System.out.println("Default Roll No.: " + ved.getRoll_no());
//
// System.out.println();
//
// // After setting attributes default value changes
// ved.setName("Vedant");
// ved.setRoll_no(45);
// System.out.println("Name: " + ved.getName());
// System.out.println("Roll No.: " + ved.getRoll_no());
// Commented out to run different forms of Constructor
System.out.println();
// Giving argument to a constructor:
studentClass ved = new studentClass("Yug");
System.out.println(ved.getName()); // Note the argument is now printed in Get Name as default
System.out.println(ved.getRoll_no());
// Note just like a method it can also take up as many args u want to give.
// Also Note: Constructor Overloading is same as Method Overloading
// That is : Same name and different args
}
}