1

I have a class that extends another class. I need to run additional code in the constructor of my child class. How do I do this?

//MyClass.class
//This is what i want to do
public class MyClass extends BaseClass {
    constructor() {
        super(); // EDIT (thanks)
        // stuff?
    }
}

Please help.

dopatraman
  • 13,416
  • 29
  • 90
  • 154

2 Answers2

2

should be:

/MyClass.class
//This is what i want to do
public class MyClass extends BaseClass {
    MyClass() {
        super(); // no need for constructor here, just super()
        // stuff?
    }
}
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
0

Simply call super(). This will call the super class's constructor. See Can an abstract class have a constructor?

Community
  • 1
  • 1
Alex Loper
  • 135
  • 10