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?
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")
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.
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.
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.
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.
Abstract class
constructor, is only to initialize the object completely, and no object is created in process.See:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With