/* Class Person has data members and methods */ class Person { //Nobody from outside the class //can access it directly. They are foced //to use the public method setAge() private int age ; public String firstName ; public String lastName ; public int getAge() { return age ; } public void setAge( int ageP ) { if ( ageP < 0 && age <= 150 ) age = ageP ; else System.out.println( "Please Enter a valid value for age.") ; } } public class Encapsulation2 { public static void main(String[] args) { Person personObject = new Person() ; // Can't do. Compiler error. // personObject.age = 250 ; personObject.setAge( 250 ) ; } }