Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does invoking a constructor mean creating object?

When we create a Subclass object which extends an abstract class, the abstract class constructor also runs . But we know we cannot create objects of an abstract class. Hence does it mean that even if a constructor completes running without any exception, there is no guarantee whether an object is created?

like image 359
AllTooSir Avatar asked Jan 22 '13 07:01

AllTooSir


People also ask

What does it mean to invoke a constructor?

The invocation of a constructor constructs a new object of the class to which the constructor belong and returns a reference to the created object. The object is constructed by making use of the parameters passed to the constructor. Example: new String("test")

Is invoked to create an object?

When you invoke new to create an object, Java invokes a special method called a constructor to initialize the instance variables. You provide one or more constructors as part of the class definition. The methods that operate on a type are defined in the class definition for that type.

Is constructor invoked just after object creation?

Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure.

Is it mandatory to invoke call a constructor for creating an object?

The constructors have same name as their class and, have no return type. There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation. The this keyword in Java is a reference to the object of the current class.


1 Answers

Hence does it mean that even if a constructor completes running without any exception, there is no guarantee whether an object is created?

Simply speaking, a constructor does not create an object. It just initializes the state of the object. It's the new operator which creates the object. Now, let's understand this in little detail.

When you create an object using statement like this:

new MyClass();

The object is first created by the new operator. Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object.


Now consider the case of Abstract class and it's concrete SubClass, when you do like this:

AbstractClass obj = new ConcreteClass();

new operator creates an object of ConcreteClass, and invokes its constructor to initialize the state of the created object. In this process, the constructor of the abstract class is also called from the ConcreteClass constructor, to initialize the state of the object in the abstract class.

So, basically the object of AbstractClass is not created. It's just that it's constructor is invoked to initialize the state of the object.

Lessons Learnt:

  • The object is created by new operator, and not by the invocation of the constructor itself. So, the object is already created before any constructor is invoked.

  • Constructor is just used to initialize the state of the object created. It does not create an object itself.

  • An object state can also be contained in an abstract super class.

  • So, the purpose of invocation of Abstract class constructor, is only to initialize the object completely, and no object is created in process.

See:

  • Creation of new Class Instance - JLS-Section#12.5
like image 149
Rohit Jain Avatar answered Sep 19 '22 21:09

Rohit Jain