Classes for children that don't require a parent constructor

I am programming in Eclipse and found something strange ...

If I create a class like this:

public abstract class Test {
    public Test(Object obj) {}
}

      

Any child class obviously needs to populate super()

.

The same for:

public abstract class Test {
    public Test(Object[] obj) {}
}

      

So why does my IDE not complain when I do the following and provide it super()

?

public abstract class Test {
    public Test(Object... obj) {}
}

      

And before you say, "Just run the program, it won't work," I used JUnit and the program worked fine without providing a constructor in the extending class. My question is, why is this happening?

The child class looks like this:

public class Child extends Test {
    // Should throw error.
}

      

+3


source to share


1 answer


With this method, public Test(Object... obj)

you can enter 0 or more parameters. By default, if a constructor does not explicitly call the parent constructor, it super()

will be implicitly called. You don't need to call it explicitly because the syntax is Object... obj

used for the general case, whether you are passing an argument or not. If you want to force the user to pass in a parameter, you just need to declare the argument as List

.



+5


source







All Articles