0% found this document useful (0 votes)
245 views2 pages

Encapsulation Constructor Overloading

This document contains code examples demonstrating encapsulation and constructor overloading in Java. It defines a Student class with private member variables for ID, name, and age, along with getter and setter methods. It includes a default constructor that initializes the variables, and a parameterized constructor that initializes the variables using passed parameters. The code then creates Student objects using both the default and parameterized constructors and prints out the member variable values to show constructor overloading in action.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
245 views2 pages

Encapsulation Constructor Overloading

This document contains code examples demonstrating encapsulation and constructor overloading in Java. It defines a Student class with private member variables for ID, name, and age, along with getter and setter methods. It includes a default constructor that initializes the variables, and a parameterized constructor that initializes the variables using passed parameters. The code then creates Student objects using both the default and parameterized constructors and prints out the member variable values to show constructor overloading in action.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Example code for 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());
}
}

You might also like