Does invoking a constructor mean creating object?

前端 未结 8 1483
情书的邮戳
情书的邮戳 2020-12-06 05:26

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

相关标签:
8条回答
  • 2020-12-06 05:38

    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.

    0 讨论(0)
  • 2020-12-06 05:38

    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 讨论(0)
  • 2020-12-06 05:41

    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
    0 讨论(0)
  • 2020-12-06 05:49

    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(); }}
    
    0 讨论(0)
  • 2020-12-06 05:51

    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 讨论(0)
  • 2020-12-06 05:53

    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.
    0 讨论(0)
提交回复
热议问题