Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Constructor, Java vs C++

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"?

like image 850
Adrian Shum Avatar asked Apr 16 '15 03:04

Adrian Shum


Video Answer


2 Answers

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.

like image 190
alekya reddy Avatar answered Oct 19 '22 17:10

alekya reddy


  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).

like image 41
ZachSand Avatar answered Oct 19 '22 19:10

ZachSand