When do you call super() in Java? I see it in some constructors of the derived class, but isn\'t the constructors for each of the parent class called automatically? Why woul
You would need to use super()
in a case like this:
public class Base {
public Base(int foo) {
}
}
public class Subclass extends Base {
public Subclass() {
super(15);
}
}
This is a very contrived example, but the only time that you need to call super()
is if you're inheriting from a class that doesn't provided a default, parameterless constructor. In this cases you need to explicitly call super()
from the constructor of your subclass, passing in whatever parameters you need to satisfy the base class's constructor. Also, the call to super()
must be the first line of your inherited class's constructor.
If you fail to call super in a constructor, then the compiler will add a no-argument super call as the parent constructor for the first line of the constructor body.
So as in the previously posted code
public class Foo
{
}
or
public class Foo
{
public Foo()
{
}
}
the compiler will generate code that aligns with the rules of Java. All objects are subclasses of java.lang.Object, so the compiler will compile it as if it was written
// All classes extend Object. This is Java, after all.
public class Foo extends java.lang.Object
{
public Foo()
{
// When constructing Foo, we must call the Object() constructor or risk
// some parts of Foo being undefined, like getClass() or toString().
super()
}
}
But if the super class doesn't have a constructor that matches the parameters, then you must call the appropriate non-matching super class constructor.
public class LightBlue extends java.awt.Color
{
public LightBlue()
{
// There is no Color() constructor, we must specify the suitable super class
// constructor. We chose Color(int red, int green, int blue).
super(172, 216, 230);
}
}
Other times, perhaps there is a constructor that matches the signature of the sub class's constructor, but for whatever reason, you do not wish to pass the parameters directly to the super class constructor.
public class HSVColor extends Color
{
public HSVColor(int hue, int saturation, int value)
{
super(...code converting HSV to Red...,
...code converting HSV to Green...,
...code converting HSV to Blue);
}
}
If you neglect to explicitly specify the super class constructor, then the compiler adds in the default no-arg super class constructor. However, if that constructor doesn't exist, then the compiler will not compile the class, because it is unclear which of the exposed constructors is the correct one to call.
It is a style consideration, but you may decide to always include the super() call. If you chose to do so, it will be because you want the remind the reader that the base class objects must be built as part of the sub class object, and you want to make the particular super class constructor explicit. Traditionally, an always included super() call is quite odd, since traditionally code is only typed out if it differs from "default" behaviour.
Because the super class' constructor is not called automatically. There might, for example, be several constructors, some of which take additional parameters. So you do not always have got an "empty" super() statement, but something like this:
public class DerivedClass extends SomeOtherClass
{
private String anotherParameter;
public DerivedClass(int parameterA, String parameterB, String anotherParameter)
{
super(parameterA, parameterB);
this.anotherParameter = anotherParameter;
}
}
Edit: I obviously forgot to say (or my choice of words simply wasn't good, but I'm no native speaker, sorry for that) that if the super class does not take any parameters, the call to super() will be done for you by java/the compiler. (Now that I re-read my answer, I can see that it really sounds like you would always have to call super().)
Super is called when we want to inherit some of the parameters from the parent class. It is not compulsory as the compiler always call the default constructor. If you want to inherit a constructor having some parameters then you need to call the super. So you can use it.
You may call super() with parameters if you want to call a non-default constructor of the superclass, or if the superclass does not have a default constructor. The compiler can only insert the default, no arguments super() constructor.
When you have a class that extends another class and the father class have no default constructor then you have to use super() in the Son's constructor to call the constructor in the Father Class with the proper arguments like this :
class A
{
int a;
A(int value)
{
this.a=value;
System.out.println("A Constructor " + a );
}
}
class B extends A
{
int b;
B()
{
super(5);
this.b=10;
System.out.println("B Constructor " + b);
}
}
you have to know that yo can't use "super" with "this" if you want to call another constructor in the calss using "this".