// What happens when passing a reference to an object?
// Here, encapsulation may cause some confusion.

class Data1 {
    private int k;
    Data1(int k) {
	this.k = k;
    }
    public int test1(Data1 d) {
	return k + d.k;

	// Hey, how can I say d.k??? Isn't k private?
	// Yes, but d has the same class as method test1.
	// Java allows direct access if the class is the same.

    }

    public int test2(Data2 d) {

	// Now, d has type Data2 which does NOT match Data1.
	// So, the following line of code won't work!
	// return k + d.k;

	// This line will:
	return k + d.get_k();
    }

}

class Data2 {
    private int k;
    Data2(int k) {
	this.k = k;
    }
    public int get_k() {
	return k;
    }
}    


public class encaps1 {
    public static void main(String args[]) {
	
	Data1 d1 = new Data1(1);
	Data1 d2 = new Data1(2);
	Data2 d3 = new Data2(3);

	System.out.println(d1.test1(d2));
	System.out.println(d1.test2(d3));
	
    }

}

/* Output: 
3
4
*/
