8

The term "Default Constructor" is official in both Java and C++ and it seems meaning different thing in the two worlds. May I know if my understanding is correct and what is the proper naming of some related concepts?

  1. Default Constructor in Java means the (No-arg) Constructor generated by Compiler when there is no constructor defined for a class.

  2. Default Constructor in C++ means a constructor that can be called with no argument.

Given the following example

Java:

class NoCtorClass {
    // No ctor defined, compiler is generating one   --------- J-1
}

class NoArgCtorClass {
    public NoArgCtorClass() { ... } -------------------------- J-2
}

C++:

class NoCtorClass {
    // implicitly NoCtorClass() constructor is provided -------- C-1
}

class DefaultCtorClass {
public:
    // Explicitly telling compiler to give the default one
    DefaultCtorClass() = default;  ---------------------------- C-2
}

class NoArgCtorClass {
public:
    NoArgCtorClass();  ----------------------------------------- C-3
}
NoArgCtorClass::NoArgCtorClass() {....}


class NoArgCtor2Class {
public:
    NoArgCtor2Class(int i = 0); -------------------------------- C-4
}
NoArgCtor2Class::NoArgCtor2Class (int i = 0) {....}

in Java, only J-1 is officially called default constructor, while J-2 is not.

In C++, all C-1 to C-4 are officially called default constructor.

Is my understand correct?

If so, some questions in terminology:

  1. What is the proper name in Java for ctor without argument? (i.e. J-1 and J2). I usually call it No-Arg Constructor. (For which corresponds to the concept of default-ctor in C++)

  2. What is the proper name in C++ for ctor generated by compiler? (i.e. C-1 and C-2. With the keyword default, it seems should be called default. Then should it be called "default default constructor"? (For which corresponds to the concept of default-ctor in Java)

  3. Similar to 2, how should we call the compiler generated Copy-ctor, assignment operator, and etc? "Default-Copy-Constructor"?

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • [This reference might help](http://en.cppreference.com/w/cpp/language/default_constructor). And a compiler generated default constructor is a compiler generated default constructor, there's no real name to it. – Some programmer dude Apr 16 '15 at 03:30
  • @JoachimPileborg "defaulted default constructor". – T.C. Apr 16 '15 at 03:32
  • 2
    I suspect `C++` gets its term `default constructor` from situations when the object creator can not specify constructor arguments (eg. when creating an array) and therefore the `default` (no arg necessary) constructor is called. This is not an issue in `Java` when object arrays always contain pointers, not the objects themselves. – Galik Apr 16 '15 at 03:40
  • 2
    In C++, the default constructor is the one called by default when you declare a variable like `MyClass x;` without any constructor args or initializers. In Java, you can't really declare a variable like that since all variables are references--`MyClass x;` just sets `x` to `null`. So the C++ meaning of "default constructor" wouldn't be applicable at all to Java. – ajb Apr 16 '15 at 03:42
  • Actually I have same thought as Galik and ajb. Just to make sure my concept is clear and doesn't mixed up for both language – Adrian Shum Apr 16 '15 at 03:59
  • btw, I am interested to know the reason for downvote :) Just to make sure that I can improve next time (if it is a valid reason :P ) – Adrian Shum Apr 16 '15 at 04:02
  • It was already discussed in [many posts](http://stackoverflow.com/questions/4488716/java-default-constructor) before – Sigismundus Apr 16 '15 at 04:31
  • I am focusing in similarity and difference of default constructor in Java and C++, for which I think is different from what were asked before (and I have given the definitions in my question which align with the commonly accepted definition) – Adrian Shum Apr 16 '15 at 04:33

2 Answers2

1

In java both are called default constructors. The Java compiler internally generates the the constructor with no args. if you specify the constructor with no args , its like you are overriding the constructor.

public NoArgCtorClass() { ... }

For example what ever code you keep in the { ... } will get executed when you instantiate the object...

Note : if you have a overloaded constructor , you need to explicitely write the default constrcutor, since the compilor doesnt generate no args constructor in this case.

alekya reddy
  • 934
  • 1
  • 8
  • 14
  • 3
    *"In java both are called default constructors."* No, they aren't. http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8.9 – Radiodef Apr 16 '15 at 03:31
  • @Radiodef you're reading it wrong: *- The default constructor has the same accessibility as the class, The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class, - The default constructor has no throws clauses, - If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments* – Luiggi Mendoza Apr 16 '15 at 03:40
  • 1
    @LuiggiMendoza I think what you quoted is irrelevant: it is about "the characteristics of a default constructor", instead of saying "constructor bearing such characteristic is a default constructor". First line in the quoted document : "If a class contains no constructor declarations, then a default constructor is implicitly declared" seems implies to me that a is ctor generated when no ctor is declared. And we call such kind of generated ctor "default constructor" – Adrian Shum Apr 16 '15 at 03:48
  • 1
    @AdrianShum it's more clear in the example below that explanation: *where **the default constructor** is public because the class Point is public*. I don't see how this is irrelevant at all. – Luiggi Mendoza Apr 16 '15 at 03:49
  • It simply says the generated ctor (aka default ctor) follows the accessibility of the class. However it does not says for ctors that follow the accessibility of the class (and with bunch of characteristic) is a default ctor – Adrian Shum Apr 16 '15 at 03:52
  • Another proof from JLS: *It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.* If "default-ctor" is the name for no-arg no-throw ctor, then the line should be wrote as *It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible default constructor*. However it chose to use that long-verbose-name, which seems implies to me default constructor does mean that – Adrian Shum Apr 16 '15 at 03:54
  • 1
    @LuiggiMendoza Even if your interpretation of the specification is right, my original statement is still correct because the OP's constructor for the package-private class `NoArgCtorClass` is public. *"The default constructor has the same accessibility as the class."* And this answer is still wrong. – Radiodef Apr 16 '15 at 03:59
  • @AdrianShum *"default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause"* This doesn't invalidate Luiggi's argument, because the superclass constructor described does not need to conform to the form of a default constructor, only be accessible. – Radiodef Apr 16 '15 at 04:08
  • @Radiodef that's true :). Then even if Luiggi's interpretation is correct, for a non-public class declaring a no-arg public ctor, we cannot call it a "default ctor" isn't it? :P – Adrian Shum Apr 16 '15 at 04:09
  • @AdrianShum That is what my earlier comment stated, yes, presumably. – Radiodef Apr 16 '15 at 04:15
  • @Radiodef Cool. I am still not convinced by the Luiggi's interpretation on the JLS though :) – Adrian Shum Apr 16 '15 at 04:16
  • 1
    @AdrianShum I have never seen the JLS use *default constructor* to explicitly refer to anything other than a compiler-generated one but I think it's safe to say we've learned there is not one interpretation. – Radiodef Apr 16 '15 at 04:18
  • That's true. The only way to find out the actual meaning is by asking Oracle and related committee I think :P – Adrian Shum Apr 16 '15 at 04:27
  • I have asked this question as new one on SO, but some readers marked it as duplicate and it is closed now. It is define [HERE](http://stackoverflow.com/questions/29681791/can-i-define-default-constructor-in-java). If you could help to unmarked it it could be re-open again. – Sigismundus Apr 16 '15 at 18:39
1
  1. The Java docs - "If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared". If a no arg constructor is user defined it is still referred to as the default constructor according to the JLS. In practice programmers don't always stick to this convention as it can be ambiguous (some use no arg constructor, etc.)

  2. In my experience, both C1 and C2 versions of constructors (implicit and explicit) are both referred to as default constructors.

  3. The compiler generated default methods for a class are generally called the following; Default constructor, Copy constructor, Copy assignment and Destructor. It is usually just the no argument constructor that receives the prefix "default" even though all methods are given by default (empty class).

ZachSand
  • 143
  • 8
  • *In other words, it varies* I cannot see this. The same segment of the JLS you link provides an example where **it clearly states** this: *where the **default constructor** is `public` because the class `Point` is public* – Luiggi Mendoza Apr 16 '15 at 03:45
  • 1
    For point 2, by definition in C++, C1 and C2 are default constructor, **as well as C3 and C4** – Adrian Shum Apr 16 '15 at 04:00
  • @LuiggiMendoza If you're saying that the JLS is calling the explicitly declared constructor a "default constructor", you're reading it wrong. The second code snippet in example 8.8.9-1 is not an example of a default constructor; it's the object of the phrase "is equivalent to". That is, the _first_ snippet, which has an implicit constructor, **is equivalent to** the second snippet--that's why the second snippet is there. This example is _not_ trying to show two examples of a default constructor. I'm not sure whether that's what you were thinking--but if so, you're reading it wrong. – ajb Apr 16 '15 at 04:41
  • @ajb both pieces of code compile the same bytecode, so both pieces are just the same, and both are examples of default constructor, and the text at the bottom of the first example is very clear on this. I don't know how can this be misread. – Luiggi Mendoza Apr 16 '15 at 04:43
  • @LuiggiMendoza "same bytecode" is irrelevant. We're talking about how a term is defined, and language definitions **do** and **must** define terms that make statements about how the source of a program is formed, without making statements about the generated bytecode. Please trust me on this--I have lots of experience working on a commercial compiler and have made lots of contributions to another (not Java) language standard. – ajb Apr 16 '15 at 14:51