Search code examples
javamethodssubclasssuperclassinherited

I don't understand output of My program, Explain please


I have 4 class and 1 interface
In interface have 2 abstract methods (myMethod(int k) and getV())

public interface MyInterface
{ public abstract void myMethod(int k);
 public abstract int getV();
} 

In MySuperClass that implements MyInterface

public class MySuperClass implements MyInterface
{ private int v;
 public MySuperClass() { this(2); }
 public MySuperClass(int vValue) { v = vValue; }
 public void myMethod(int k) { v += k; }
 public void myMethod() { v--; }
 public int getV() { return v; }
 public String toString() { return Integer.toString(v); }
} 

In MySubClass that extends MySuperClass

public class MySubClass extends MySuperClass
{ private int v;
 public MySubClass() { this(1); }
 public MySubClass(int vValue) { v = vValue; }
 public void myMethod(int k) { myMethod(); super.myMethod(k); }
 public int getV() { return v; }
 public String toString() { return super.toString() + " " + Integer.toString(v); }
} 

MyOtherClass implements MyInterface

public class MyOtherClass implements MyInterface
{ private int v;
 public MyOtherClass() { this(0); }
 public MyOtherClass(int vValue) { v = vValue; }
 public void myMethod(int k) { v-= k; }
 public void myMethod() { v++; }
 public int getV() { return v; }
 public String toString() { return Integer.toString(v); }
} 

In the main class

public class MyMain {
    public static void main(String[] args) {
    MyInterface[] mif
                = {new MySuperClass(), new MyOtherClass(), new MySubClass()};

        mif[mif[2].getV()].myMethod(1);
        for (int i = 0; i < mif.length; i++) {
            System.out.println(mif[i]);
        }
        mif[mif[0].getV()].myMethod(2);
        for (int i = 0; i < mif.length; i++) {
            System.out.println(mif[i]);
        }}}

output
2
-1
2 1
2
-1
3 1

I don't understand line 6 is 3 1, why not 4 0

I think output is 4 0 because
mif[mif[0].getV()].myMethod(2); => mif[2].myMethod(2); => MySubClass.myMethod(2);
and public void myMethod(int k) {myMethod(); super.myMethod(k); }
I think 'myMethod()' change 'v' as 1 so v = 0 and super.myMethod(k) change 'v' as 2 so v = 4


Solution

  • I'm assuming you've understood lines 1 - 3.

    At this point, MySuperClass.v = 2, MySubClass.v = 1, MyOtherClass.v = -1

    I'm gonna refer to them as Super, Sub and Other.

    Now, mif[0].getV() returns 2. Therefore, mif[2].myMethod(2) calls MySubClass.myMethod(2), which in turn calls myMethod() and Super.myMethod(2)

    Now, myMethod() the value of Super.v to 1, and Super.myMethod(2) changes the value of Super.v from 1 to 3

    At this point, Super.v = 3, Sub.v = 1, Other.v = -1

    So, line 4 gives Super.v = 3,
    Line 5 gives Other.v = -1,
    Line 6 gives Super.v Sub.v = 3 1

    You might have noticed that the outputs I've explained are different from the current post, and that's because I'm 100% sure OP has copied the outputs into his question wrong.