Designer Overload Benefits

I am very new to Java and try to learn a theme with previous programming experience only in HTML / CSS. I started with Herbert Schildt and advanced a few pages.

I cannot understand the exact benefits of constructor overloading. Is it not easy to overload methods with a single constructor for flexibility? Moreover, if I try to use constructor overload to use one object to initialize another, there are easier ways to do this! So what are the benefits and in what situation should I use constructor overload.

+6
source share
6 answers

Constructor overloading is very useful for simulating default values ​​or for creating an object from an existing instance (copy)
Here is an example:

public class Color { public int R, G, B, A; // base ctr public Color(int R, int G, int B, int A) { this.R = R; this.G = G; this.B = B; this.A = A; } // base, default alpha=255 (opaque) public Color(int R, int G, int B) { this(R, G, B, 255); } // ctr from double values public Color(double R, double G, double B, double A) { this((int) (R * 255), (int) (G * 255), (int) (B * 255), (int) (A * 255)); } // copy ctr public Color(Color c) { this(cR, cG, cB, cA); } } 

Here, the first constructor is very simple. You specify the R, G, B, and Alpha values ​​for the color.

As long as this is enough to use the Color class, you provide a second constructor, a liter, which automatically assigns 255 alpha if not specified by the user.

The third ctr shows that you can create an instance of the Color object with a double value from 0. to 1. instead of integers.

The latter takes color as a unique argument; it copies the given object.

The benefits are also that the first constructor is always called, and you can use it to manually count your instances. Let's say you have a private static int count=0 attribute private static int count=0 , you can track the number of Color instances as follows:

  // base ctr public Color(int R, int G, int B, int A) { this.R = R; this.G = G; this.B = B; this.A = A; ++count; } 

count incremented from any constructor.

+5
source

Given the simplest example of constructor overloading:

 Class A { int a; A() { this.a = 0; } A(int a) { this.a = a; } } // end class 

Advantage: Now I may just need to use the default constructor new A() to assign default values ​​or, for a more dynamic case, specify which value should be new A(10) , which is a parameterized constructor. read this question

Isn't it easier to overload methods with a single constructor for flexibility?

The task of the designer is to create an instance of the object, and the task of the method is to process the values ​​of the object. Thus, limited processing methods (an exception to setter methods ) and a constructor for creating an object will help in the long run, and will also make your class more convenient for your teammates

Also, if I try to use constructor overload to use one object to initialize another, there are simpler ways to do this!

alternative to initializer , also readable

+2
source

It all depends on how you build your object.

One classic example of constructor overloading is the ArrayList in Java. ArrayList has three constructors: one is empty, the other accepts the collection object, and the other the initial capacity. this overloaded constructor allows the flexibility to create an arraylist object. You may not know the size of the arraylist at creation time than you can simply use the default argument constructor without arguments, but if you know the size, it is best to use an overloaded constructor that accepts capacity. Since an ArrayList can also be created from a different collection, it can be from a different list than another overloaded constructor, it makes sense. Using the overloaded constructor, you can convert your ArrayList to Set or any other collection.

Additional information with common examples.

+1
source

A way of defining a constructor will ensure that constructor parameters are passed to the object when the instance is created.

Method overloading will not force anyone to call it, unlike parameters in the constructor, where you cannot initiate an object without passing these parameters.

It would be useful to use several methods of constructing an object, for example, with the Color class, in the ndj answer in four different ways. Each of the methods ensures that Color as the minimum information that will be useful, or "work."

This information may not be available if Color had only a constructor without a parameter, and for the "work" class there would be a call to setR , and other methods ... that do not exist in <class href = "http://docs.oracle.com/javase /1.4.2/docs/api/java/awt/Color.html "rel =" nofollow "> Color.

+1
source

When you are dealing with immutable classes , and you want to provide several ways to create them, the constructor overload is convenient.

0
source

You may have several variables in the class ... But you want to initialize only a few (one or two) variables during the creation of the object. Therefore, in this case, it is always better to have overloaded constructors so that you can call the appropriate constructor, depending on your requirement. One thing that you should always remember when overloading constructors is always to provide a default constructor (constructor of zero arguments). Even with an empty implementation.

0
source

Source: https://habr.com/ru/post/952499/


All Articles