dhust
1/19/2017 - 8:23 PM

Encapsulation example. Need to make the age variable private so no one can directly change it, and the setAge() method is checking for a val

Encapsulation example. Need to make the age variable private so no one can directly change it, and the setAge() method is checking for a valid age.

public class EncapsulationExample {
	
	// variables
	private int age;
	
	// default constructor
	public EncapsulationExample() {
		
	}

	// encapsulation
	public void setAge (int newAge) {
		
		if (newAge < 0) {
			System.out.println("Invalid age");
		} else {
			age = newAge;
		}
		
	}
	
}