How jvm handles abstract class in java

一曲冷凌霜 提交于 2019-12-02 09:53:48

JVM cannot instantiate an abstract class it can instantiate only an instance of a non-abstract subclass of abstract class. Creating an instance means allocating space in memory necessary to hold all non-static fields and then initialing these fields by calling constructors.

Sujith PS

JVM can not instantiate Interface or Abstract classes.

Take a look at this .

In the JVM, every object has a pointer to its class, but only to its concrete class and not to its interface or abstract class. If we get the memory address of an object, we can get the address of its class easily. This method is useful only for classes whose instances can be created. Neither interfaces nor abstract classes can be used in this way.

Refer this for more details .

In 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.

if Abstract class object is not created internally by the JVM, but only its constructor is invoked to initialise the state. Then without instantiation what is the point in initialisation and to which object initialisation is performed.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!