forked from chetannihith/Java-hacktoberfest25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverloading_1.java
More file actions
71 lines (58 loc) · 2.11 KB
/
Overloading_1.java
File metadata and controls
71 lines (58 loc) · 2.11 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
package hacktoberfest;
class Student {
// Class data members
private String rollno;
private String name;
private int marks;
private String address;
// Default constructor (non-parameterized)
public Student() {
this.rollno = "Not Assigned";
this.name = "Unknown";
this.marks = 0;
this.address = "Not Available";
}
// Parameterized constructor with name and rollno as parameters
public Student(String name, String rollno) {
getData(name, rollno);
}
// Parameterized constructor with name, rollno, marks, and address as parameters
public Student(String name, String rollno, int marks, String address) {
getData(name, rollno, marks, address);
}
// Method-1 to set name and rollno
public void getData(String name, String rollno) {
this.name = name;
this.rollno = rollno;
}
// Method-2 to set name, rollno, marks, and address
public void getData(String name, String rollno, int marks, String address) {
getData(name, rollno); // Previous method reused to set name and rollno
this.marks = marks;
this.address = address;
}
// Method-3 to print the student's details
public void getData() {
System.out.println("Roll no.: " + rollno);
System.out.println("Name: " + name);
System.out.println("Marks: " + marks);
System.out.println("Address: " + address);
}
}
public class Overloading_1 {
public static void main(String[] args) {
// Three student objects
Student stud1 = new Student();
Student stud2 = new Student("Tony", "S3000");
Student stud3 = new Student("Steve", "R1945", 97, "New York");
// Printing details of the student objects
System.out.println("Student-1 Details -");
stud1.getData();
System.out.println();
System.out.println("Student-2 Details -");
stud2.getData();
System.out.println();
System.out.println("Student-3 Details -");
stud3.getData();
}
}