Thor
Thor

Reputation: 10038

When invoking the super method, what is i referring to?

I'm trying to learn the concept of super. Could someone please tell me what the i in super(i) is referring to?

Is it the private int variable i in the NaturalNumber class? Is it the parameter in the NaturalNumber? Is it referring to something else? I'm very confused by the syntax.

 class NaturalNumber {

    private int i;

    public NaturalNumber(int i) { this.i = i; }
    // ...
}

class EvenNumber extends NaturalNumber {

    public EvenNumber(int i) { super(i); }
    // ...
}

Upvotes: 0

Views: 74

Answers (6)

Pshemo
Pshemo

Reputation: 124215

super(arguemnts) is call to superclass constructor to which you pass arguments.
In case of class EvenNumber extends NaturalNumber it is mechanism which ensures NaturalNumberness of your class.

What happens in this case is that you are passing to superclass constructor same value you passed to EvenNumber class via public EvenNumber(int i). So it will initialize private int i field (which you don't have direct access to from EvenNumber since it is private).

Maybe you will better see it if we rename variables a little:

class NaturalNumber {

    private int value;

    public NaturalNumber(int naturalValue) {
        this.value = naturalValue;
    }
    // ...
}

class EvenNumber extends NaturalNumber {

    public EvenNumber(int oddValue) {
        super(oddValue);
    }
    // ...
}

So when you create instance of EvenNumber via new EvenNumber(2) first thing which happens in EvenNumber(int oddValue) constructor is super(2) which will invoke NaturalNumber(int naturalValue) constructor and pass 2 to it, which finally will set int value to 2.

Upvotes: 2

Makoto
Makoto

Reputation: 106400

The i in the super expression refers exclusively to the local variable i inside of your NaturalNumber class. It does not expand its scope any further beyond that.

Upvotes: 1

Khalil Hamani
Khalil Hamani

Reputation: 91

The super() is supposed to be called with the same parameters as the Constructor of the super-class, as it does the exact same thing. For example :

    class Person {
        private name;

        public Person(String name) {
            this.name = name;
        }

        public Person() { /* Empty constructors initialize with default values,
                              i.e in this case name is initialized with null*/
        }
    }

    class Employee extends Person {
        private double salary;

        public Employee(double salary) {
            super("Michael");
            this.salary = salary;
            /* That means that the emplyee which is created is a person
               who's name is "Michael"*/
        }

        public Employee(double salary) {
            super();
            this.salary = salary;
            /* That means that the emplyee which is created is a person
               who's name is null as the empty constructor initialized it*/
        }
    }

Hope this helped.

Upvotes: -1

Anoop Kanyan
Anoop Kanyan

Reputation: 618

class NaturalNumber {

private int i;

public NaturalNumber(int x) { this.i = x; }
// ...
}

class EvenNumber extends NaturalNumber {

public EvenNumber(int y) { super(y); }
// ...
}

Consider the code above. This code does the exact same thing as your code. I changed variable names to make it clear. So, when you call super(y), what it does is call it's parent class's constructor, which is NaturalNumber(i), the value of y is passed on to this constructor, and inside this constructor you are setting the value of variable i equal to the value passed to constructor.

So when we call super(int), what it does is pass an integer value to the parent class's constructor. Also, to set some variable of a parent class we can do super.i=10 which will set the variable 'i' in the parent class to 10.

Upvotes: 1

Kevin Kang
Kevin Kang

Reputation: 13

When you call super(i), it will call the superclass's constructor. The super keyword refers to the superclass (parent class). In this case, the superclass of your EvenNumber is NaturalNumber because Even number extends NaturalNumber. So basically, by saying super(i) you are saying NaturalNumber(i).

Upvotes: 1

sisyphus
sisyphus

Reputation: 6392

In your example it is the constructor parameter of EvenNumber. The call super(i) passes the constructor parameter to the superclass constructor (by value, because it's a primitive). The superclass constructor is then storing that value in a private field.

Upvotes: 3

Related Questions