EXPERIMENT NUMBER:01
Date of Performance:
Date of Submission:
AIM: Write a Java program to demonstrate the use of class and
objects
THEORY:
Class and Object in Java
In Java, a class is a blueprint or template that defines the properties
(attributes) and behaviors (methods) that objects of that class will have.
An object, on the other hand, is an instance of a class that is created in
memory at runtime. It represents a real-world entity and can access the
fields and methods defined in its class.
Encapsulation in Java
Encapsulation is one of the fundamental principles of object-oriented
programming in Java. It refers to the bundling of data (variables) and
methods (functions) that operate on the data into a single unit, typically a
class. The main purpose of encapsulation is to protect the internal state of
an object from unintended or harmful modification.
Data Hiding in Java
Data hiding is a concept closely related to encapsulation, and it focuses
on restricting access to the internal details of a class. It is implemented
by making class members private so that they cannot be accessed
directly from outside the class. This ensures that sensitive or critical
information is hidden from unauthorized access and modification. By
exposing only the necessary parts of an object through public methods,
data hiding increases security, reduces system complexity, and allows
changes in the internal implementation without affecting external code
that uses the class
SE/B2/SUNISH PANIGRAHY/ROLL NO 42/EXP 1
EXPERIMENT NUMBER:1A
SOURCE CODE:
class Student{
int rollno;
String name;
public void printDetails()
System.out.println("Roll No:"+rollno);
System.out.println("NAME:"+name);
}
public class Studentdemo {
public static void main(String[] args){
System.out.println("The student details");
Student dhiraj = new Student();
sunish.rollno = 42;
sunish.name = "Sunish Panigrahy";
sunish.printDetails();
}
}
OUTPUT:
D:\>javac Studentdemo.java
D:\>java Studentdemo
The student details
Roll No:42
NAME: Sunish Panigrahy
CONCLUSION: In this program we learned the concept of class and object
allows Java to model real-world entities. In the student example, the
Student
SE/B2/SUNISH PANIGRAHY/ROLL NO 42/EXP 1
class serves as a blueprint, and the object student1 represents a specific
student with a name.
EXPERIMENT NUMBER:1B
AIM: Write a program to demonstrate encapsulation of data.
SOURCE CODE:
public class Person{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Person person = new Person();
person.setName("Shawn Michael");
System.out.println("Person's name is: " + person.getName());
}
}
OUTPUT:
D:\>javac Person.java
D:\>java Person
Person's name is: Shawn Michael
CONCLUSION: This program clearly illustrates the concept of
Encapsulation
SE/B2/SUNISH PANIGRAHY/ROLL NO 42/EXP 1
in java.
EXPERIMENT NUMBER:1C
AIM: Write a program to demonstrate Data Hiding.
SOURCE CODE:
public class Bank{
private double balance = 1000.0;
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawal successful.");
}
else: {
System.out.println("Insufficient balance.");
}
System.out.println("Balance: " + balance);
}
Bank account = new Bank();
account.withdraw(500);
}
}
OUTPUT:
D:\>javac Bank.java
D:\>java Bank
Withdrawal successful.
Balance: 500.0
SE/B2/SUNISH PANIGRAHY/ROLL NO 42/EXP 1
CONCLUSION:
In this program we learned about Data Hiding how to protect the
important details of a class. By keeping data private.
MARKS & SIGNATURE:
SE/B2/SUNISH PANIGRAHY/ROLL NO 42/EXP 1