Constructor

Constructor
Constructor

Constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object’s data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting object in a valid state. Immutable objects must be initialized in a constructor.

Most languages allow overloading the constructor in that there can be more than one constructor for a class, with differing parameters. Some languages take consideration of some special types of constructors. Constructors, which concretely use a single class to create objects and return a new instance of the class, are abstracted by factories, which also create objects but can do so in various ways, using multiple classes or different allocation schemes such as an object pool.

Use:

We use constructors to initialize the object with the default or initial state. The default values for primitives may not be what are you looking for. Another reason to use construct is that it informs about dependencies.

Why do we use constructor overloading?

The constructors are overload to initialize the objects of a class in different ways. This allows us to initialize the object with either default values or used given values. If data members are not initialize then program may give unexpected results.

Can a constructor return a value?

There are no “return value” statements in the construct, but the construct returns the current class instance. We can write ‘return’ inside a construct. Like methods, we can overload constructors for creating objects in different ways.

Can constructor be static?

One of the important property of java construct is that it can not be static. We know static keyword belongs to a class rather than the object of a class. A construct is call when an object of a class is creating, so no use of the static construct.

Can constructor be private?

Yes. Class can have private construct. Even abstract class can have private construct By making construct private, we prevent the class from being instantiate as well as sub classing of that class.

Can you call a constructor?

Invoking a construct from a method

No, you cannot call a construct from a method. The only place from which you can invoke constructors using “this()” or, “super()” is the first line of another construct. If you try to invoke constructors explicitly elsewhere, a compile time error will be generated.

What is the purpose of a private constructor?

Private constructors are use to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is call to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.

Can abstract class have constructor?

The construct inside the abstract class can only be call during construct chaining i.e. when we create an instance of sub-classes. This is also one of the reasons abstract class can have a construct.

Can we override static method?

No, we cannot override static methods because method overriding is base on dynamic binding at runtime and the static methods are bond using static binding at compile time. If we call a static method by using the child class object, the static method of the child class will be called.