Home > Back-end >  What's the point of a default constructor in OOP?
What's the point of a default constructor in OOP?

Time:01-19

I'm relatively new/inexperienced when it comes to computer science. I was wondering what the point of having a default constructor is when you can just declare any fields you want in your class and initialize them with whatever default value you want them to have. Then, you can write overloaded constructors.

To get an idea of what I'm talking about, here's an example.

public class Restaurant {
      String name = "No name";
      int Rating = -1;
}

versus

public class Restaurant {
     String name;
     int rating;
     public Restaurant() {
           name = "No name";
           rating = -1;
     }
           

CodePudding user response:

Constructors are basically used for making objects. As far as variable initialization is concerned, when you initialize a variable (instance variable), you change the variable which is a member of the object you just created. You will understand it better by the example given below (your eg itself):

public class Restaurant {
   String name;
   int rating;
   public Restaurant() {
       name = "No name";
       rating = -1;
   }
}

Now suppose you create an object,

Restaurant obj = new Restaurant();

Here, obj.name = "No name" and obj.rating=-1. Note that name and obj.name are different. Now when you use the first code given by you,

public class Restaurant {
    String name = "No name";
    int Rating = -1;
}

Now whichever object you create, variable name of every object will contain "No name" only.

Thus, when you have to work with objects, you should initialize the instance variable through constructors. By the way, instance itself means object...

CodePudding user response:

When your variable initialization is based on some logic, you need to write some code and for that purpose default constructor is required. Java provides another approach called initializer blocks and you will be able to do the same but the consistency is the key. The constructors are for object construction let it handle the construction from code readability and maintainability perspective.

CodePudding user response:

IMHO the default constructor is not a requirement of OOP. This is more of a requirement of Java: each class must have at least one constructor. This is underlined by the JLS: 8.8.9 Default Constructors:

If a class contains no constructor declarations, then a default constructor is implicitly declared.

If your class contains at least one constructor declaration (as in your second example) the default constructor is not generated.


Note that when you write

public class Restaurant {
    String name = "No name";
    int Rating = -1;
}

the field initializers name = "No name"; and Rating = -1; are executed as part of the constructor (JLS 12.5 Creation of New Class Instances):

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

This initialization code is added to every constructor - whether it is the default constructor or any constructor that you write.

  •  Tags:  
  • Related