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.
Abstract classes can have private methods. Interfaces can't. Abstract classes can have instance variables (these are inherited by child classes).
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.
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.
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.
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
B.class
objectObject
instance variables (including helper fields for GC, the monitor for synchronisation,...)A
instance variables (only a
in this case)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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With