Encapsulation Constructor Overloading
Encapsulation Constructor Overloading
Student.java
public class Student
{
private String stuID;
private String stuName;
private int stuAge;
Student()
{
//Default constructor
stuID = "25DNS15F2024";
stuName = "Haslinda Ali";
stuAge = 18;
}
Student(String stuID, String stuName, int stuAge)
{
//Parameterized constructor
this.stuID = stuID;
this.stuName = stuName;
this.stuAge = stuAge;
}
//Getter and setter methods
public String getStuID() {
return stuID;
}
public void setStuID(String 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;
}
}
TestStudent.java
class TestStudent
{
public static void main(String args[])
{
//This object creation would call the default constructor
Student stud = new Student();
System.out.println("Student Name is: "+stud.getStuName());
System.out.println("Student Age is: "+stud.getStuAge());
System.out.println("Student ID is: "+stud.getStuID());
System.out.println();
/*This object creation would call the parameterized
* constructor StudentData(int, String, int)*/
Student stud2 = new Student("18DNS15F1016", "Syahrizan", 25);
System.out.println("Student Name is: "+stud2.getStuName());
System.out.println("Student Age is: "+stud2.getStuAge());
System.out.println("Student ID is: "+stud2.getStuID());
}
}