16

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?

1
  • Thank you for the question. even i had similar doubts May 21, 2014 at 6:26

8 Answers 8

21

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:

2
  • Is there a way to create an object of a class without using the new operator?
    – minion
    Sep 24, 2015 at 8:39
  • 1
    @minion No, there's isn't any way.
    – Rohit Jain
    Sep 25, 2015 at 16:35
3

But we know we cannot create objects of an Abstract class

Right but JVM can.

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

The object is definitely created internally.

Does invoking a constructor mean creating object?

Not always. you can invoke constructor using super() and this() but it won't instantiate an object. (but will just invoke the constructor)

class AClass
{
    AClass()
    {
        this(1); // will invoke constructor, but no object instatiated.
    }

    AClass(int a)
    {

    }

    public static void main(String[] args)
    {
        AClass obj = new AClass(); // one object instantiated.
    }
}
1
  • 3
    Note: - Invoking a constructor never creates an object. It's the new operator that does it. So, your argument to 3rd quote is wrong.
    – Rohit Jain
    Jan 22, 2013 at 7:25
2

Barring any exceptions, the abstract class constructor is only run from within the subclass's constructor (as the first statement). Therefore you can be sure that every time a constructor is run, it is in the process of creating an object.

That said, you may call more than one constructor in the process of creating a single object, such as Subclass() calling Subclass(String) which calls AbstractClass via a super()call, and so forth.

3
  • it is in the process of creating an object. -> No, it is in process of initializing an object. Object is already created using new operator.
    – Rohit Jain
    Jan 22, 2013 at 7:19
  • @RohitJain "Each of these situations identifies a particular constructor (§8.8) to be called with specified arguments (possibly none) as part of the class instance creation process." (JLS 12.5) Jan 22, 2013 at 7:28
  • @JeffBowman.. Well, JLS included the inialization of object in object creation itself. So, neither of us are wrong. Appologies. :)
    – Rohit Jain
    Jan 22, 2013 at 7:32
2

Subclass == BaseClass + Extras you add in sub class

Thus when you create a subclass by calling its constructor, there is a call to base class constructor as well to make sure that all attributes (of the base class) are also properly initialized.

0

You can only call the abstract class constructor as a part of a concrete subclass constructor. This is OK, since the abstract class is extended into a concrete class and it is an object of that concrete class that is being created.

0

When you invoke a constructor using new, a new object is being created.

Now, as you probably already know, every constructor of any subclass, either implicitly or explicitly, directly or indirectly, invokes a constructor from the parent class (which, in turns, invokes one from its own parent class, all the way up to object). This is called constructor chaining.

The above, however doesn't mean that multiple objects are created. The object has been created at the new call and all constructors working on that object are already handed an allocated area. Therefore, constructor chaining does not create new objects. One call to new will return you one object.

0

This is how the flow works when you invoke the constructor of your subclass:

  1. Constructor of Abstract class runs --> the object is half initialized here.
  2. Constructor of subclass finishes execution --> The object is fully initialized and hence completely created here.
3
  • Note that Object are already completely created before the constructor is invoked.
    – Rohit Jain
    Jan 22, 2013 at 7:20
  • @Rohit: I meant the object is half baked/ half initialized at this point in time :) I hope this is correct ? Jan 22, 2013 at 7:29
  • @NiranjanBhat.. Hmm. Now it's quite fine.
    – Rohit Jain
    Jan 22, 2013 at 7:36
0

I am completely disagree that the object for an abstract class can not be created and only jvm can does it in case of Inheritance even a programmer can do it a t times whenever or wherever he/she intends to do so by using the concept of anonymous class: Look at this code and try it by your Own

abstract class Amit{ 
void hai()
{System.out.print("Just Wanna say Hai");}
abstract void hello();
}
class Main{
stic public void main(String[]amit)
{
Amit aa=new Amit(){
void hello(){Sstem.out.print("I actually dont say hello to everyone");
}};
aa.hello(); }}
1
  • Please don't post your your personal e-mail Dec 5, 2013 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.