My question is if we can access and use indirectly/implicitly created super class objects when we instantiate a subclass .
Lets say ClassA
is super class of SubClassofA
and we instantiate SubClassofA
in a client class ClientClass
using SubClassofA object = new SubClassofA();
Since whole inheritance hierarchy gets instantiated when we create a subclass objects, I was wondering, is it possible to access object of class ClassA
in client class?
If not possible, what might be reasons? Wouldn't it save lots of heap memory if we can access super class objects without recreating those?
I might have misunderstood whole concept of constructor chaining and inheritance hierarchy but please let me know your thoughts on this.
public class ClassA {}
public class SubClassofA extends ClassA {}
public class ClientClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClassofA object = new SubClassofA();
//Above construct means that an instance of super class ClassA exists too
// If we can use those super class instances directly, will it result in memeory saving?
//is it even possible to access implicitly created super class objects tied to subclass?
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…