Prasad Kharkar wrote:@Anchit
it it true that we cannot instantiate abstract classes
but the abstract class constructor is called when a subclass of abstract class is instantiated
this is because constructor has implicit call to super constructor
Please tell me why that needs to happen?
Suppose class B is extending class A:
During instantiation of class B object I understand that constuctorB will implicitly call constructorA. This is because we are not only creating everything specified in class B but also other members such as private methods and private variables of class A also need to exist in the memory. For example: Class A has a public method display() that uses its private variables. We extend class A and call it class B and add a few more methods. Now we create an instance of class B. We know that we can invoke display() method on B object. How is this possible? It can only happen when everything defined in A is also present in the memory. Hence in B's constructor even if we don't call super() we know that implicitly that call will still occur.
Okay I just now realized that maybe the same principle applies to the case
Class B implements abstract class A . One trivial clarification regarding this:
What happens in the following case:
abstract Class A
|
| (B extends A)
|
abstract Class B
|
| (C extends B)
|
abstract Class C
|
| (D implements C)
|
Class D <== we instantiate an object for D. Here is it true that constructor for D calls constructor for C which in turn calls its super all the way till A? I think that that is what should happen.