Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

What is the correct way to initialize a member of a superclass?

if I have a class that has, for instance a HashMap that is initialized in the constructor like so...

public class MySuper{
   HashMap<String,String> foo;

   public MySuper(){
      foo = new HashMap<String,String>();
   }
}

my impression (which is incorrect) is that I just need to call super in the subclass and the same initialization will take place, but Eclipse isn't happy with the following syntax...

 public class MySub extends MySuper{
      public MySub(){
         super.MySuper()
      }
   }

So, I'm curious what the correct way to do this is. Do I have to create a separate method in MySuper that does the initialization and call super on that? (I know that will work), but I was under the impression that there was a way to just call super on the class I'm extending from and have it do whatever it would normally do in its constructor.

TIA

Upvotes: 1

Views: 135

Answers (5)

Pau Kiat Wee
Pau Kiat Wee

Reputation: 9505

By default, subclass's default contructor will call super class's default contructor, so you can just write

public MySub(){
    // super(); is automatically called at first line, so you no need call it
    // child init
}

or you can call superclass's constructor in first line of child constructor

public MySub(){
    super();
    // child init
}

If super class have constructor public MySuper(int i) that you wish to call, you can write

public MySub(){
    super(100);
    // child init
}

Upvotes: 1

Himanshu Mohta
Himanshu Mohta

Reputation: 1043

You have no need to call super.MySuper() because super classe's default constructor always invoke implicitly because the first line of child class's constructor is super() by default.

public class MySuper{
   HashMap<String,String> foo;

   public MySuper(){
      foo = new HashMap<String,String>();
   }
}

public class MySub extends MySuper{

}

Upvotes: 1

Nivas
Nivas

Reputation: 18334

You generally dont have to do anything.

The super class no argument constructor is automatically called when a subclass constructor is called.

If you do not have a no argument constructor in the super class, or you want to call another overloaded version then you have to explicitly call via super (otherwise your code won't compile, because the super class constructor has to be called, and if the default no-arg constructor is not available, the VM would not know which overload to call).

You do it this way:

super() or

 public class MySub extends MySuper{
      public MySub(){
         super();
      }
   }

or

 public class MySub extends MySuper{
      public MySub(int a, int b){
         super(a, b);
      }
   }

See using super.

Upvotes: 1

Thomas Calc
Thomas Calc

Reputation: 3014

The parameterless constructor of the superclass is called automatically from the subclass.

But if you want to emphasize it in your code, you can use:

 public class MySub extends MySuper{
      public MySub(){
         super();
      }
   }

super() must be the first statement of your constructor body.

Upvotes: 3

hiroprotagonist
hiroprotagonist

Reputation: 902

you can just call super() -- also, fyi, in your particular example, you don't even have to write a constructor in the sub-class, it will implicitly call the super class default constructor

Upvotes: 2

Related Questions