class TestPrivate {
private String s;
public TestPrivate () {
s = “Actual String”;
}
public void print() {
System.out.println("S is: "+s);
}
public void change(TestPrivate tp) {
tp.s = tp.s + ” Modified”;
}
public static void main (String args[]) {
TestPrivate s1 = new TestPrivate ();
TestPrivate s2 = new TestPrivate ();
s1.print();
s2.change(s1);
s1.print();
}
}
Answer: The output is
ReplyDeleteActual String
Actual String Modified
As per OOP concepts, a private modifier restricts access of a member or function outside the class in which the element is defined. So there is no violation of OOP concepts here. It is a perfectly valid scenario.