0% found this document useful (0 votes)
113 views5 pages

Encapsulation Demo

The document defines a Student class with attributes like student ID, name, phone number, address, degree, and year of passing. It includes two constructors - one that initializes ID, name, and phone number and another that initializes ID, name, and email. Methods are included to get/set attribute values and print student details. The main method creates two Student objects to demonstrate the class.

Uploaded by

Sharada Mani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views5 pages

Encapsulation Demo

The document defines a Student class with attributes like student ID, name, phone number, address, degree, and year of passing. It includes two constructors - one that initializes ID, name, and phone number and another that initializes ID, name, and email. Methods are included to get/set attribute values and print student details. The main method creates two Student objects to demonstrate the class.

Uploaded by

Sharada Mani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java supports two types of relationship

1. IS A relationship
2. HAS A relationship
The IS A relationship defines the inheritance of a superclass to
the subclass where HAS A relationship defines composition of a
class.

Assignment:
Create Student class with following attributes.
Student_ID, Student_Name, Student_PhoneNumber,
Student_MailId, Student_Address, Degree & YOP (year of
passing).
Class should allow to create a student with
1. Student_Id ,Student_Name & Student_PhoneNumber
2. Student_Id,Student_Name & Student_MailId
Class should have methods to print every attribute values
The student class should provide an option to modify
phoneno, MailId & Address
Provide method to print details of the student
1. Student Class
2. Attributes-Id, StudentName,
StudentNumber,StudentMailId,StudentAddress,Degre
e & YOP
3. Constructors
a) StudentId,StudentName &
StudentPhoneNumber
b) StudentId,StudentName & StudentMailId
4. Method to get each & every attribute values
5. Modify StudentPhoneNo,MailId & Address

class Student
{
//attributes
private int studentId;
private String studentName=null;
private long studentPhoneNumber;
private String studentAddress=null;
private String studentMailId=null;
private String studentDegree=null;
private int YOP;

//Constructors
Student(int studentId,String studentName,long
studentPhoneNumber)
{
this.studentId=studentId;
this.studentName=studentName;
this.studentPhoneNumber=studentPhoneNumber;
}
Student(int studentId,String studentName,String
StudentMailId)
{
this.studentId=studentId;
this.studentName=studentName;
this.studentMailId=studentMailId;
}
int getStudentId()
{
return studentId;
}
String getStudentName()

{
return studentName;
}
long getStudentPhoneNumber()
{
return studentPhoneNumber;
}
String getStudentMailId()
{
return studentMailId;
}
String getStudentAddress()
{
return studentAddress;
}
String getStudentDegree()
{
return studentDegree;
}
int getStudentYOP()
{
return YOP;
}
void setStudentPhoneNumber(long studentPhoneNumber)
{
this.studentPhoneNumber=studentPhoneNumber;
}

void setStudentMailId(String studentMailId)


{
this.studentMailId=studentMailId;
}

void setStudentAddress(String studentAddress)


{
this.studentAddress=studentAddress;
}

void printStudentDetails()
{
System.out.println("Student Id:" + studentId);
System.out.println("Student Name:" + studentName);
System.out.println("Student PhoneNo:" +
studentPhoneNumber);
System.out.println("Student MailId:" + studentMailId);
System.out.println("Student Address:" + studentAddress);
System.out.println("Student Degree:" + studentDegree);
System.out.println("Student YOP:" + YOP);
}
}

class CreateStudent
{
public static void main(String[] args)
{
Student s1=new
Student(1,"YoyoHoneySingh",987654321l);

Student s2=new
Student(2,"AamirKhan","[email protected]");

System.out.println(s1.getStudentName());
s1.setStudentPhoneNumber(80000000);
System.out.println(s1.getStudentPhoneNumber());
s2.printStudentDetails();
}
}

You might also like