-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructorOverloading.java
More file actions
67 lines (57 loc) · 1.52 KB
/
ConstructorOverloading.java
File metadata and controls
67 lines (57 loc) · 1.52 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
package Learn;
class ConstructorOverloading
{
private int stuID;
private String stuName;
private int stuAge;
ConstructorOverloading()
{
stuID = 100;
stuName = "New Student";
stuAge = 17;
}
ConstructorOverloading(int num1, String str, int num2)
{
stuID = num1;
stuName = str;
stuAge = num2;
}
/* The get method returns the variable value, and the set method sets the value.
However, as the name variable is declared as private, we cannot access it from outside this class. */
//Getter and setter methods
public int getStuID()
{
return stuID;
}
public void setStuID(int stuID)
{
this.stuID = stuID;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public static void main(String args[])
{
ConstructorOverloading obj = new ConstructorOverloading();
System.out.println("Student Name is: "+obj.getStuName());
System.out.println("Student Age is: "+obj.getStuAge());
System.out.println("Student ID is: "+obj.getStuID());
ConstructorOverloading obj2 = new ConstructorOverloading(555, "Chat", 25);
System.out.println("Student Name is: "+obj2.getStuName());
System.out.println("Student Age is: "+obj2.getStuAge());
System.out.println("Student ID is: "+obj2.getStuID());
}
}