0

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

micky
  • 3
  • 2

1 Answers1

1

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.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
  • Thank you very much, but I really don't understand why the value of myMethod() is super.v not sub.v (sorry for my question is not obvious and my gramma) – micky May 04 '19 at 14:12
  • @micky MySubClass inherits both the methods and the variables from MySuperClass. This means that MySubClass.myMethod(2) calls Super.myMethod() and Super.myMethod(2) one after the other, so the value of Super.v is changed from 2 to -1, then from -1 to 3 – Robo Mop May 04 '19 at 14:15
  • @micky If your question has been answered satisfactorily, please mark this answer as accepted, and upvote it so that others may also be able to refer to this question :) – Robo Mop May 04 '19 at 14:16