0

What the following code does is to try and store the minimum value which would be added to the base stack inside another stack which in my example is called s2(and when that min value would be popped,it would also be popped from the Stack s2), what I don't understand is how does base.Push(value); in the push method and base.Pop(); in the pop method work?

My super class is Stack<int> which means I can use its methods including push and pop but in my example if I use my push method on an Integer which will call the base.Push on that Integer, but where does that base.Push method adds that Integer since the only stack that I created and initialized is the Stack s2

class StackWithMin2 : Stack<int> {

    Stack<int> s2;

    public StackWithMin2() {
        s2 = new Stack<int>();
    }

    public void push(int value) {
        if (value<=min()) {
            s2.Push(value);
        }

        base.Push(value);
    }

    public int pop() {
        int value = base.Pop();
        if (value==min() {
            s2.Pop();
        }
        return value;
    }

    public int min() {
        if (s2.Count == 0)
        {
            return int.MaxValue;
        }
        else {
            return s2.Peek();
        }

    }
}
dymanoid
  • 14,771
  • 4
  • 36
  • 64
Daniel_Kamel
  • 610
  • 8
  • 29
  • 1
    You also invoke the parameterless constructor on `base` class so it probably initializes there. – Vidmantas Blazevicius Sep 23 '19 at 14:48
  • 2
    It pushes it to `this`. – phuzi Sep 23 '19 at 14:48
  • 1
    Why are you creating a `Stack` (`s2`) in a `Stack`? – mm8 Sep 23 '19 at 14:51
  • @VidmantasBlazevicius how's that? – Daniel_Kamel Sep 23 '19 at 14:52
  • Here is an explanation https://stackoverflow.com/questions/13166019/will-the-base-class-constructor-be-automatically-called – Vidmantas Blazevicius Sep 23 '19 at 14:54
  • @mm8 the ```stack s2``` is the stack which will keep record of the minimum values that will be added to the ```base```,but what I don't understand is that ```base``` is used to call the methods of the superclass which in this case is ```Stack``` but I never declared or initialized another stack other than s2, so where does ```base``` push and/or pop the values when I call ```base.Push(anyInteger)``` or ```base.Pop()```? – Daniel_Kamel Sep 23 '19 at 14:55
  • 5
    `StackWithMin2` *is-a* stack. That's what inheritance *means*. It also *has-a* another stack, `s2`. That's composition. – Damien_The_Unbeliever Sep 23 '19 at 14:57
  • @VidmantasBlazevicius is that the same case with java if I do the same and call ```super.AMethod()```? – Daniel_Kamel Sep 23 '19 at 14:58
  • 1
    `StackWithMin2` inherits from `Stack`. By initializing an instance of `StackWithMin2` you are actually initializing two instances of `Stack`. The private field `StackWithMin2.s2` and the instance of `StackWithMin2` itself. When you call `base.Push`, you are pushing the `int` onto the private storage that `StackWithMin2` gets by nature of inheriting `Stack`. – Joshua Robinson Sep 23 '19 at 14:58
  • Ahhh I see,thanks guys :) – Daniel_Kamel Sep 23 '19 at 14:58
  • @user007: From your `StackWithMin2` stack. – mm8 Sep 23 '19 at 14:59

0 Answers0