JVM是否在内部实例化抽象类的对象? [英] Does the JVM internally instantiate an object for an abstract class?

查看:204
本文介绍了JVM是否在内部实例化抽象类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类及其具体的子类,当我创建一个子类的对象时,它会自动调用超级构造函数。 JVM在内部创建抽象类的对象吗?

I have an abstract class and its concrete subclass, when I create an object of subclass it automatically calls the super constructor. Is the JVM internally creating an object of the abstract class?

public abstract class MyAbstractClass {

    public MyAbstractClass() {
        System.out.println("abstract default constructor");
    }

}
public class ConcreteClass extends MyAbstractClass{

    public static void main(String[] args) {
        new ConcreteClass();
    }

}

然后构造函数如何在没有对象的情况下存在在JVM中(对于抽象类)

then how constructor exists without an object in JVM ?? (In case of abstract class)

在创建对象之后也执行构造函数,然后在不创建抽象类对象的情况下如何执行默认构造函数? (在 Java文档

Also constructor gets executed after object is being created then without creating the object of abstract class how the default constructor get executed ?? (This is mentioned in Java Doc)

推荐答案


您确实无法实例化抽象类吗?

Is it true that you can't instantiate abstract class?



是。

Yes.


JVM是在内部创建对象吗?抽象类?

Is JVM internally create object of abstract class ?

不是,但这是一个常见的误解(并且这样做不是不合理的方式;典型的

No, but it's a common misunderstanding (and that wouldn't be an unreasonable way for it to be done; prototypical languages like JavaScript do it that way).

JVM会创建一个对象,该对象属于您创建的类(在您的情况下, ConcreteClass )。它从其超类( MyAbstractClass )和其子类( ConcreteClass )中获得该对象的某些方面,但是只有一个对象。

The JVM creates one object, which is of the class you created (in your case, ConcreteClass). There are aspects of that one object that it gets from its superclass (MyAbstractClass) and from its subclass (ConcreteClass), but there is only one object.

该对象是其所有部分的集合,包括似乎具有相同名称的部分,例如超类的方法被子类覆盖。实际上,这些方法具有完全限定的名称,并且彼此之间没有冲突,这就是为什么可以调用超类的重写方法版本的原因。

The object is an aggregate of all of its parts, including parts that seem to have the same name, such as a method of the superclass that is overridden by the subclass. In fact, those methods have different fully-qualified names and don't conflict with one another, which is why it's possible to call the superclass's version of an overridden method.

因此,如果只是一个对象,为什么会看到对 MyAbstractClass 的构造函数的调用?在我们回答之前,我需要提到Java编译器正在做的一些事情,您在源代码中看不到这些东西:

So if it's just one object, why do you see the call to MyAbstractClass's constructor? Before we answer that, I need to mention a couple of things the Java compiler is doing that you don't see in the source code:


  1. 正在为 ConcreteClass 创建一个默认构造函数。

  1. It's creating a default constructor for ConcreteClass.

在该构造函数中,它调用 MyAbstractClass 构造函数。

In that constructor, it's calling the MyAbstractClass constructor.

仅此而已:在 MyAbstractClass 构造函数,它向超类的( Object )构造函数添加了一个调用,因为没有 super(...) MyAbstractClass 构造函数中编写的c $ c>调用。

Just to be thorough: In the MyAbstractClass constructor, it's adding a call to the superclass's (Object) constructor, because there's no super(...) call written within the MyAbstractClass constructor.

以下是Java编译器为您添加的代码的代码内容:

Here's what the code looks like with the bits the Java compiler adds for you filled in:

public abstract class MyAbstractClass {

    public MyAbstractClass() {
        super();           // <== The Java compiler adds this call to Object's constructor (#3 in the list above)
        System.out.println("abstract default constructor");
    }

}
public class ConcreteClass extends MyAbstractClass{

    ConcreteClass() {      // <== The Java compiler adds this default constuctor (#1 in the list above)
        super();           // <== Which calls the superclass's (MyAbstractClass's) constructor (#2 in the list above)
    }

    public static void main(String[] args) {
        new ConcreteClass();
    }

}

好吧,方式,让我们触摸 TheLostMind 在评论中非常有用的一点:构造函数不要创建对象,它们初始化它们。 JVM创建对象,然后根据需要针对该对象运行尽可能多的构造函数(它们实际上应称为 initializers ),以使每个超类都有机会初始化其部分

Okay, with that out of the way, lets touch on a point TheLostMind very usefully mentioned in a comment: Constructors don't create objects, they initialize them. The JVM creates the object, and then runs as many constructors (they really should be called initializers) against that one object as necessary to give each superclass a chance to initialize its part of the object.

因此在该代码中,会发生什么(您可以在调试器中逐步了解它)以了解它:

So in that code, what happens (and you can step through this in a debugger to fully understand it) is:


  1. JVM创建对象

  1. The JVM creates an object

ConcreteClass 构造函数称为


  1. 构造函数要做的第一件事是超类的构造函数,在本例中为 MyAbstractClass 的构造函数。 (请注意,这是绝对的要求:在超类构造函数调用之前,Java编译器不允许您在构造函数本身中包含任何逻辑。)

  1. The first thing that constructor does is call its superclass's constructor, in this case MyAbstractClass's constructor. (Note that this is an absolute requirement: The Java compiler will not allow you to have any logic in the constructor itself prior to the superclass constructor call.)


  1. 构造函数要做的第一件事是调用其超类的构造函数( Object 's)

  1. The first thing that constructor does is call its superclass's constructor (Object's)

Object 构造函数返回时, MyAbstractClass 构造函数的其余部分运行

When the Object constructor returns, the remainder of the MyAbstractClass constructor runs


  • MyAbtractClass 构造函数返回时, ConcreteClass 构造函数的其余部分运行

  • When the MyAbtractClass constructor returns, the remainder of the ConcreteClass constructor runs


  • 对象是 new ConcreteClass()表达式的结果。

    请注意,如果存在带有初始化程序的实例字段,则上述内容将变得更加复杂。有关完整的详细信息,请参见JLS和JVM规范。

    Note that the above would get more complicated if there were instance fields with initializers. See the JLS and JVM specs for the full details.

    这篇关于JVM是否在内部实例化抽象类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆