Skip to content

Instantly share code, notes, and snippets.

@omatt
Created September 19, 2013 06:40
Show Gist options
  • Save omatt/6619800 to your computer and use it in GitHub Desktop.
Save omatt/6619800 to your computer and use it in GitHub Desktop.
Simple implementation of the Object Oriented Concept - Encapsulation
/**
* Sample for Object Oriented Programming Concepts
*
* Encapsulation
*
* @author Reyes, Omar Matthew B.
* @version 2013/09/07
*/
public class EncapsulationSample{
private String name;
private String address;
private int age;
// Objects that allows private variables be accessed by other classes
/** "Getter" methods or accessor **/
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public int getAge(){
return age;
}
// Set variable values that can be accessed from other classes.
/** "Setter" methods or mutator **/
public void setName(String newName){
name = newName;
}
public void setAddress(String newAddress){
address = newAddress;
}
public void setAge(int newAge){
age = newAge;
}
}
/**
* Sample for Object Oriented Programming Concepts
*
* Encapsulation
*
* @author Reyes, Omar Matthew B.
* @version 2013/09/07
*/
public class RunEncapsulation{
public static void main(String [] args){
EncapsulationSample encap = new EncapsulationSample();
encap.setName("Omar");
encap.setAddress("Quezon City");
encap.setAge(20);
System.out.println("Name: " + encap.getName() + "\nAddress: "
+ encap.getAddress()+"\nAge: "+encap.getAge());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment