Sengo
Sengo

Reputation: 53

Difference between super(variableName) ; and super.variableName

enter image description here What is the Difference between super(variable-name); and super.variableName = something; in a constructor, when you want to initialize the parameters and you wanna assign one of them to a variable of a parent class?

for example i want to implement the constructor of the "Zahnradfraese" and it takes the parameter "int Kennung" and this parameter should be assigned to the attribute "kennung" of the parent class "Produktionmittel"

Should I always use super when I wanna call a variable from this parent class or I just use it if I have another variable with the same name in the child class?

Upvotes: 0

Views: 486

Answers (5)

YK S
YK S

Reputation: 3440

super(variable_name) represents a constructor call and should be first line in the constructor. Whereas super.variableName = something; means you are assigning a value to the instance variable of the parent class from the child class using super which is used to refer parent class objects.

Now in your case: as per given class-diagram the class Zahnradfraese has a constructor which takes int Kennung argument. Also, kennung is the parent-class and has no constructor and instead it has method setKennung(). So you can do super.setKennung(kennung) from inside the constructor of Zahnradfraese class. You can also declare a constructor inside kennung but that would mean deviating from the class-diagram which has setter and getter methods and no constructor.

public class Zahnradfraese extends Kennung{
  public Zahnradfraese(int kennung){
    super.setKennung(kennung);
  }
}

Upvotes: 1

Andrew
Andrew

Reputation: 49606

What is the difference between super(variableName); and super.variableName = something;?

method() (here, super(variableName)) is a method invocation (here, a parent's constructor invocation).

super.variableName = something; is an assignment to a parent's field.

Should I always use super when I wanna call a variable from this parent class or I just use it if I have another variable with the same name in the child class?

super(variableName) can initialise the inner state of the parent, particularly super.variableName. It is reasonable to initialise a super.variableName before accessing it. Both ways you listed can be utilised for that. Just make sure there is no code duplication.

I want to implement the constructor of the Zahnradfraese and it takes the parameter int Kennung and this parameter should be assigned to the attribute kennung of the parent class Produktionmittel.

Add a constructor to Produktionmittel which takes an int

public Produktionmittel(int i) {
    kennung = i;
}

and call it from the child:

public Zahnradfraese(int kennung) {
    super(kennung);
}

Upvotes: 2

dognose
dognose

Reputation: 20899

super(arg) invokes the constructor of the super class, setting the variable just sets the variable. (The constructor might contain more logic than just assigning a variable, which you bypass with the second way)

Simple example:

public class P{
  protected String variable1;
  private boolean variableInitialized = false;

  public P (String s){
     this.variable1 = s;
     this.variableInitialized=true;
  }
}

public class C extends P{

}

calling super("x") within C will also set the boolean flag, as the parent class "might expect" it. Calling super.variable1="x" will not affect the boolean flag, and you can't change it, cause it's private.

As a rule of the thumb i'd say: If there is a dedicated constructor for a certain variable, it seems worth using it, unless you exactly want to override that implementation.

Upvotes: 1

Shubhendu Pramanik
Shubhendu Pramanik

Reputation: 2751

super() is a keyword which is used to call the constructor in the parent class and it must be called from inside the constructor of the child class. Also it must be the first statement.

Where as super.s is used to set the variable s (which is declared in the parent class) from the child class and it doesn't have restrictions as above.

See below example:

class Test {
    int s;

    Test(int d) {
    }
}

class T extends Test {
    T() {
        super(8);
        int d = 99;
        super.s = 00;
    }

    void ss() {
        super.s = 99;
    }
}

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 40058

So super(variableName) is invoking your parent class one arg constructor, and that logic gets executes

super.variableName = something; is assigning something value to parent class variable variableName

Upvotes: 1

Related Questions