Thursday, August 12, 2010

Test your Java knowledge

What you think will be the output of the below code be?

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 ();

1 comment:

  1. Answer: The output is

    Actual 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.


Blog Archive