Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 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. As the instance variable a of class A is private it will not be inherited by the class B . So where does the instance variable a is created(on heap) . Had it been a public it would have been a part of class B instance on Heap. Also class A is an abstarct class so it can not be instantiated.

like image 917
Pankaj Avatar asked Dec 12 '13 09:12

Pankaj


People also ask

Can an abstract class have private instance variables?

Abstract classes can have private methods. Interfaces can't. Abstract classes can have instance variables (these are inherited by child classes).

Are instance variables stored in heap?

Instance variables are declared in the class, but outside of the constructors, methods, or blocks of the particular class. They are used to represent the state of an object. Instance variables are stored in the heap section of the memory.

What are instance variables variables that are private to the class?

The instance variables are visible for all methods, constructors, and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers. Instance variables have default values.

How do I access private instance variables?

We have used the getter and setter method to access the private variables. Here, the setter methods setAge() and setName() initializes the private variables. the getter methods getAge() and getName() returns the value of private variables.


2 Answers

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
  2. block of Object instance variables (including helper fields for GC, the monitor for synchronisation,...)
  3. block of A instance variables (only a in this case)
  4. block of B instance variables (none in this case)

however each implementation of a JVM is free to choose how it allocates each of them

and access control is enforced by both the compiler and the JVM

like image 144
ratchet freak Avatar answered Oct 12 '22 03:10

ratchet freak


Instance of variable 'a' would be created on heap inside object of class 'B'. and, it would still be created inside object of class 'B' where instance 'a' being public.

OBJECT of a subclass must contain its superclass's private fields. Having no access to a private member doesn't mean its not there.

As the JLS states.

Members of a class that are declared private are not inherited by subclasses of that class. 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.

The sub-class doesn't private fields of super class. OBJECTS of subclasses contain private fields of their superclasses. The subclass itself has NO NOTION of private fields of its superclass.

like image 25
Aditya Avatar answered Oct 12 '22 02:10

Aditya