Difference between super(variableName) ; and super.variableName
Difference between super(variableName) ; and super.variableName
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?
super(variable-name);
super.variableName = something;
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?
super.setKennung(kennung)
Zahnradfraese
5 Answers
5
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.
super(variable_name)
super.variableName = something;
super
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.
Zahnradfraese
int Kennung
kennung
setKennung()
super.setKennung(kennung)
Zahnradfraese
kennung
public class Zahnradfraese extends Kennung
public Zahnradfraese(int kennung)
super.setKennung(kennung);
Great! thanks, one more question please, in this case if i use setKennung, super will make no diffrence? i mean because Zahnradfraese is extending the Produktionmittel so it has access to its methods, right? and if i did like super.kennung = sth will do the same job?
– Sengo
Sep 16 '18 at 17:05
@Sengo you are right. In your case the child class has access to parent class method so you can do that.
– Yug Singh
Sep 16 '18 at 17:11
What is the difference between super(variableName);
and super.variableName = something;
?
super(variableName);
super.variableName = something;
method()
(here, super(variableName)
) is a method invocation (here, a parent's constructor invocation).
method()
super(variableName)
super.variableName = something;
is an assignment to a parent's field.
super.variableName = something;
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
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.
super(variableName)
super.variableName
super.variableName
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
.
Zahnradfraese
int Kennung
kennung
Produktionmittel
Add a constructor to Produktionmittel
which takes an int
Produktionmittel
int
public Produktionmittel(int i)
kennung = i;
and call it from the child:
public Zahnradfraese(int kennung)
super(kennung);
thank you for your answer, i didn't understand what do you mean by super(variableName) can initialise the inner state of the parent. could you please explain it more? and i dont have the option to add a constructor to the Produktionmittel
– Sengo
Sep 16 '18 at 16:46
@Sengo the inner state of an object is the state of all the fields it possesses.
– Andrew Tobilko
Sep 16 '18 at 17:08
@Sengo a single constructor argument can affect more than 1 field (for example, it can set one field and participate in calculations for another one)
– Andrew Tobilko
Sep 16 '18 at 17:11
@Sengo, if you can't add a constructor to the parent, then
super.variable = value;
is the only option.– Andrew Tobilko
Sep 16 '18 at 17:12
super.variable = value;
So super(variableName)
is invoking your parent class one arg constructor, and that logic gets executes
super(variableName)
super.variableName = something;
is assigning something
value to parent class variable variableName
super.variableName = something;
something
variableName
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.
super()
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.
super.s
s
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;
thank you for your help, so super(8) will compile the constructor of Test class?
– Sengo
Sep 16 '18 at 16:49
@Sengo, it will execute (not compile) the
Test
constructor– Andrew Tobilko
Sep 16 '18 at 17:14
Test
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.
super("x")
C
super.variable1="x"
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.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
as you cannot modify the kennung class, you can use
super.setKennung(kennung)
inside the constructor ofZahnradfraese
class.– Yug Singh
Sep 16 '18 at 16:54