Total members 11894 |It is currently Sat Nov 23, 2024 10:23 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What is Encapsulation?
-----------------------------


Encapsulation provides the basis for modularity- by hiding information from unwanted outside access and attaching that information to only methods that need access to it. This binds data and operations tightly together and separates them from external access that may corrupt them intentionally or unintentionally.


Encapsulation is achieved by declaring variables as Private in a class- this gives access to data to only member functions of the class. A next level of accessibility is provided by the Protected keyword which gives the derived classes the access to the member variables of the base class- a variable declared as Protected can at most be accessed by the derived classes of the class. Using set and get methods is an example of encapsulation.
java code
public class Person {

private String FirstName;
private String LastName;
private String Age;
private String Address;

public Person(String FirstName, String LastName, String Age, String Address) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Age = Age;
this.Address = Address;
}

public String getFirstName() {
return FirstName;
}

public String getLastName() {
return LastName;
}

public String getAge() {
return Age;
}

public String getAddress() {
return Address;
}

public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}

public void setLastName(String LastName) {
this.LastName = LastName;
}

public void setAge(String Age) {
this.Age = Age;
}

public void setAddress(String Address) {
this.Address = Address;
}

}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time
Post new topic Reply to topic  [ 1 post ] 




Topic Tags

Java OOP
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com