[go: up one dir, main page]

0% found this document useful (0 votes)
14 views3 pages

Encapsulation notes

Encapsulation in Java is a mechanism that combines data and methods into a single unit, primarily aimed at data hiding for security. It involves using private modifiers to restrict access to class variables, which can only be accessed through public setter and getter methods. The document also illustrates the concept with examples of a fully encapsulated class and the use of constructors versus setter methods for data initialization.

Uploaded by

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

Encapsulation notes

Encapsulation in Java is a mechanism that combines data and methods into a single unit, primarily aimed at data hiding for security. It involves using private modifiers to restrict access to class variables, which can only be accessed through public setter and getter methods. The document also illustrates the concept with examples of a fully encapsulated class and the use of constructors versus setter methods for data initialization.

Uploaded by

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

Encapsulation:

Encapsulation in Java is a mechanism of wrapping the data (variables) and


code (methods) together as a single unit.

Grouping machanisum is called encapsulation.


ex: class,package.

The main objective of encapsulation is data hiding to achieve security and it


is possible to hide the data by using private modifier.

If a variable declared as a private it is possible to access those variables


only inside the class is called data hiding.

Fully encapsulated class: The class contains private properties.


class Emp
{ private int eid;
private String ename;
}

In encapsulation, the variables of a class will be hidden from other classes,


and can be accessed only through the methods(setters & getters) of their current
class.

Sales Team
Sales team contains private data so that finance team can not access the data
directly.

Finance Team
Finance can access the sales data with help of sales superviser(setter &
getter)

java bean class:


1. Bean class contains private properties & public setter & getter methods.
setters are used to set the data.
getters are used to get the data.
2. it is a resuable software component any one can set the data & get the
data.

ex:
class Emp
{ //private properties
private int eid;
private String ename;
private double esal;

//setter method to set the data : setxxx() xxx = property name


public void setEid(int eid)
{ this.eid = eid;
}
public void setEname(String ename)
{ this.ename = ename;
}
public void setEsal(double esal)
{ this.esal = esal;
}

//getters methods used to get the ata : getXXX() xxx = property name
public int getEid()
{ return eid;
}
public String getEname()
{ return ename;
}
public double getEsal()
{ return esal;
}
}

class TestClient
{ public static void main(String[] args)
{ Emp e1 = new Emp();
e1.setEid(111);
e1.setEname("sajida");
e1.setEsal(5000);

System.out.println(e1.getEid()+" "+e1.getEname()+" "+e1.getEsal());

Emp e2 = new Emp();


e2.setEid(222);
e2.setEname("anand");
System.out.println(e2.getEid()+" "+e2.getEname()+" "+e2.getEsal());
e2.setEsal(2000.45);
System.out.println(e2.getEid()+" "+e2.getEname()+" "+e2.getEsal());
e2.setEsal(15000.45);
System.out.println(e2.getEid()+" "+e2.getEname()+" "+e2.getEsal());
}
}

a. Setter methods always takes the arguments.Getter methods no arguments.


b. Setter methods will set the data so no return value.
Getter methods will get the data so return type should be property type.

In java we can initialize the data in two ways,


a. using construtor
If the constructor taking five arguments mandatory we have to pass 5
arguments.
In future if we want change the particular data is not possible.

b. using setter appraoch.


We can create the object, Depends on the values we can call specefic
setter methods remaining values are stored default values.
In future if we want change the particular data call the particular
setter methods.

ex:
when we set the data using setter methods then get the data using getter methods.
When we set the data using constructor then get the data by overriding toString().

class Emp
{ //private properties
private int eid;
private String ename;
private double esal;

Emp(int eid,String ename,double esal)


{ this.eid = eid;
this.ename = ename;
this.esal = esal;
}

public String toString()


{ return "Emp id.."+eid+" Emp name "+ename+" Emp sal "+esal;
}
}

class TestClient
{ public static void main(String[] args)
{ Emp e1 = new Emp(111,"ratan",10000.45);
System.out.println(e1);

Emp e2 = new Emp(222,"durga",20000.45);


System.out.println(e2);
}
}

Assignment:
class Student
{ sid,sname,email,standard
setter methods set the data
getter methods get the data
}
class TestClient
{ public static void main(String[] args)
{ create the object set the values
get the values using getters

update the standard


update the email
get the updated values using getters
}
}

You might also like