2

In Java inheritance we can access parent class properties via the child class object, as there is a keyword extends for achieving inheritance. But my question is that we can access any class non-static Data Member or methods via that class object reference only, so in case of inheritance we make child class object and access parent property so as java rule we can access any DM(Data Member) via that class object only so in this case according to me two possibilities are there:

  1. When inheritance is taking place all the Data Member or Member Function are first copied into child scope and then we use them because now they are properties of Child so easily accessible but in this case same DM and methods are in both, child memory as well as in parent memory. That is not a good approach because if I have 100 properties in parent than all 100 are first copied into child memory and then used.

  2. As we make child object implicitly the parent class object is made by compiler and all parent class method are called via that parent class object implicitly, but according to me that is not done. There is no object creation of parent in case of inheritance in Java. You can also check this via printing ref in child class as well as on parent class both have same reference so no parent object is created.

So my question is, how properties of parent are access via child object implicitly means internally how they achieve or is there any third approach for achieving the same used in java.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
Parveen yadav
  • 2,252
  • 1
  • 21
  • 35
  • extends is the keyword – Pat B Aug 05 '15 at 17:35
  • It almost sounds like you are looking for `super`, with it you can access parent methods also the constructor (otherwise default) but only as first statement. – maraca Aug 05 '15 at 17:40
  • @maraca: Thanks for replying but i want to know how internally they access without object of parent. – Parveen yadav Aug 05 '15 at 17:47
  • possible duplicate of [Does a superclass instance also being created when creating a subclass instance?](http://stackoverflow.com/questions/26202179/does-a-superclass-instance-also-being-created-when-creating-a-subclass-instance) – Aleksi Yrttiaho Aug 05 '15 at 17:50

1 Answers1

3

Just like the word "extends" implies, the child object is an extension of the parent object. in other words, it has all the data members of the parent, plus what other data members the child adds. If the memory layout of parent object is

PPPPPP

then the child object looks like this:

PPPPPPCCCCC

When you consider your option (2), you say: "there is no object creation of parent". That is true and false at the same time. There is no separate instance of parent class, distinct from the instance of child class you're making. However, the parent instance does exist as a PART of child instance, and the parent CONSTRUCTOR is indeed called before the child constructor to initialize that part.

  • Thanks for reply, you mean that as i mention first option in my question that is Right. – Parveen yadav Aug 05 '15 at 18:06
  • Not exactly as you state it. There is nothing to copy, as there are no two distinct locations. –  Aug 05 '15 at 18:18
  • 1
    @Parveenyadav The first option is not right: There is no copying here. It is more like the second option, except that _there is no separate parent reference_. – Jeff Bowman Aug 05 '15 at 18:24
  • @JeffBowman:- would appreciate if you please define it more clearly how they access means if there is no separate parent reference and no copying in child than how actually they work. – Parveen yadav Aug 06 '15 at 17:25
  • The parent instance is part of the chuild instance. Literally. In terms of memory addresses, let's say the child instance starts at address A. The size of child instance is, say, N. Let's also say that the parent instance has the size of M (where M <= N). The first M bytes of memory at address A can be interpreted (and are interpreted) as a parent instance. –  Aug 06 '15 at 19:01