Why is super class constructor always called [duplicate]

That is how Java works. The constructors of the parent classes are called, all the way up the class hierarchy through Object, before the child class’s constructor is called. Quoting from the docs: With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called. Note: … Read more

Does “explicit” keyword have any effect on a default constructor?

Reading explanation of members : explicit char_separator(const Char* dropped_delims, const Char* kept_delims = “”, empty_token_policy empty_tokens = drop_empty_tokens) explicit char_separator() The explicit keyword for the 1st constructor requires explicit creation of objects of char_separator type. What does the explicit keyword mean in C++? covers the explicit keyword very well. The explicit keyword for the 2nd … Read more

Nicer syntax for setting default argument value to default constructor

Yes: void foo(a::really::long::type::name arg = {}); To sum up the following standard definitions: This is list initialization. Depending of the type, aggregate initialization is performed or the object is value initialized, which in turn implies default initialized or zero initialized. Some “corner” cases are when the type is a specialization of std::initializer_list or when the … Read more

Is it possible in java to create ‘blank’ instance of class without no-arg constructor using reflection?

With standard reflection, no, but there is a library that can do it for you: objenesis. It’s specifically designed to instantiate classes without default constructors, and it’s used by other serialization libraries like xstream. Note: the constructor might not be called in these cases (but that’s presumably what you want).

What is the difference between constructor “=default” and the compiler generated constructor in C++?

Dog() = default; is a user declared constructor (not to be confused with a user defined constructor). It is a defaulted default constructor. Typically you would use it when the class has other constructors but you still want the compiler to generate a default constructor (or rather a “defaulted default constructor”. This is C++ terminology … Read more