简体   繁体   English

堆中创建的抽象类的私有实例变量在哪里?

[英]Where is a private instance variable of an abstract class created in the heap?

abstract class A {
    private int a;
    public A(int x) {
        a = x;
    }
    public int getA() {
        return a;
    }
}

class B extends A {
    public B(int x) {
        super(x);
    }
}

class TestB {
    public static void main(String args[]) {
        B b = new B(5);
        System.out.println(b.getA());
    }
}

In this situation when i say B b=new B(5); 在这种情况下,当我说B b=new B(5); the super class constructor is called and the private instance variable is initialized to 5. So when i say getA() on class B 's object reffered by b it returns 5 . 调用超类构造函数并将私有实例变量初始化为5.因此,当我在class B的对象上用b表示getA() ,它返回5 As the instance variable a of class A is private it will not be inherited by the class B . 由于class A的实例变量a是私有的,因此它不会被class B继承。 So where does the instance variable a is created(on heap) . 那么实例变量a在哪里创建(在堆上)。 Had it been a public it would have been a part of class B instance on Heap. 如果它是一个public ,它将成为Heap上class B实例的一部分。 Also class A is an abstarct class so it can not be instantiated. class A也是abstarct类,因此无法实例化。

there is no difference where the instance variables are allocated no matter if they are private, public, from a super class, from a abstract super class 实例变量的分配无论是私有,公共,超类还是抽象超类都没有区别

typically the sequence will be something like 通常序列将是类似的

  1. reference to the B.class object B.class对象的引用
  2. block of Object instance variables (including helper fields for GC, the monitor for synchronisation,...) Object实例变量块(包括GC的辅助字段,同步监视器,......)
  3. block of A instance variables (only a in this case) A实例变量的块(在这种情况下只有a
  4. block of B instance variables (none in this case) B实例变量的块(在这种情况下没有)

however each implementation of a JVM is free to choose how it allocates each of them 但是,JVM的每个实现都可以自由选择如何分配它们

and access control is enforced by both the compiler and the JVM 和访问控制由编译器和JVM强制执行

Instance of variable 'a' would be created on heap inside object of class 'B'. 变量'a'的实例将在类'B'的对象内的堆上创建。 and, it would still be created inside object of class 'B' where instance 'a' being public. 并且,它仍将在类'B'的对象内创建,其中实例'a'是公共的。

OBJECT of a subclass must contain its superclass's private fields. 子类的OBJECT必须包含其超类的私有字段。 Having no access to a private member doesn't mean its not there. 无法访问私人会员并不意味着它不存在。

As the JLS states. 正如JLS所述。

Members of a class that are declared private are not inherited by subclasses of that class. 声明为private的类的成员不会被该类的子类继承。 Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared. 只有声明为protected或public的类的成员才会被声明在声明类之外的包中声明的子类继承。

The sub-class doesn't private fields of super class. 子类不是超类的私有字段。 OBJECTS of subclasses contain private fields of their superclasses. 子类的OBJECTS包含其超类的私有字段。 The subclass itself has NO NOTION of private fields of its superclass. 子类本身没有超类私有字段的NO OFTION。

All abstract does is say "you can't create this class on its own". 所有摘要都说“你不能自己创建这个类”。 Everything defined within the class works exactly the same as for a non-abstract class. 在类中定义的所有内容与非抽象类完全相同。

So in other words remove the "abstract" keyword above and the resulting compiled bytecode will look almost identical. 换句话说,删除上面的“abstract”关键字,得到的编译后的字节码看起来几乎相同。

It changes how you can use the class, not the behavior of what the class contains. 它改变了你如何使用类,而不是类所包含的行为。

Basic Rule of Inheritance!!! 基本的继承规则!!!

From oracle documentation 来自oracle文档

A subclass does not inherit the private members of its parent class. 子类不继承其父类的私有成员。 However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. 但是,如果超类具有访问其私有字段的公共或受保护方法,则子类也可以使用这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM