Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Inheriting Variables

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code, lines 12 and 13 print their parent's version of the variable (i.e 8) since the reference is being taken. i get that !

but after lines 14 and 15, why do lines 16 and 17 don't exhibit the same behaviour as in lines 12 and 13 now they print 21 and 20 ?? if 14 and 15 are commented out, 16 and 17 print the same as before  i.e. 8 !


 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:In the below code, lines 12 and 13 print their parent's version of the variable (i.e 8) since the reference is being taken. i get that !



The "c", "d", and "s" references are all declared as cat class references, so, hence, they will access the instance variable, that are part of the cat class. This part you get ... check...

shambhavi sham wrote:
but after lines 14 and 15, why do lines 16 and 17 don't exhibit the same behaviour as in lines 12 and 13 now they print 21 and 20 ?? if 14 and 15 are commented out, 16 and 17 print the same as before  i.e. 8 !



The "d", and "s" references are declared as cat class references, so, hence, line 14 and line 15, will access and assign, the instance variables that are part of the cat class. The reason for this part should be the same as the previous. So, those values will change, right?

So, if you change those variables, don't you expect them to be changed? As expected in line 16 and line 17?

Henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if a variable with the same name is present in both the parent and child classes but initialized with a different value as follows :

class parent {int a=9;}

class child extends parent{ int a=10;}
and also in the child class main method i have this :
parent p=new child();
System.out.println(p.a); // this will print the parent's value right based on the reference type

what is followed in this case is  not followed in the earlier code i posted right ?

 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry both my codes are the same !

i didn't notice !

but i have read that the reference is checked when the variable is declared with the same name in both the parent and child classes ! thats why the first time it prints the parent version's value
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code. So the compiler doesn't know and care) about the actual objects, the compiler only knows about the types of the reference variables.

Here are two very simple (and hopefully easy to remember) but very, very, very important rules (which you must know by heart):
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • Hope it helps!
    Kind regards,
    Roel

    PS. A dog extending from cat?
     
    blossom belle
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    common roel, dog extends cat was not meant literally, just for the sake of an example !       will be careful next time though !

    and then coming to my question :

    first when the dog , cat and scrap instances are created wit cat references, each of these instances have one copy of 'a' that stores their respective values assigned in their respective classes, along with a copy of cat's variable 'a' in each of them .

    next, when i access c.a, d.a and s.a , it is the inherited version that hides their instance copy .  hence , 8 8 8 is printed .

    next, i change values of d.a and s.a !! so it is the inherited copy 'a' of each of d and s that is getting changed ?
     
    Henry Wong
    author
    Posts: 23879
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    shambhavi sham wrote:
    first when the dog , cat and scrap instances are created wit cat references, each of these instances have one copy of 'a' that stores their respective values assigned in their respective classes, along with a copy of cat's variable 'a' in each of them .



    No. A dog instance has two 'a' instance variables. One as part of the dog class, and one as part of its superclass (cat). A scrap instance has two 'a' instance variables. One as part of the scrap class, and one as part of its superclass (cat).

    Assigning the instance to a reference has no affect on the instance.

    shambhavi sham wrote:
    next, when i access c.a, d.a and s.a , it is the inherited version that hides their instance copy .  hence , 8 8 8 is printed .



    Using cat references to access the instance will access the cat version of the instance variable. Hence, the first time it is accessed, it has the value of 8.

    shambhavi sham wrote:
    next, i change values of d.a and s.a !! so it is the inherited copy 'a' of each of d and s that is getting changed ?



    Using cat references to access the instance will access the cat version of the instance variable. Hence, it is the cat version of the instance variable that will be changed.

    As mentioned in my previous post, this has nothing to do with the dog or scrap version of the instance variable. You are printing the cat version, you are changing the cat version, and then, you are printing the cat version again.

    Henry
     
    blossom belle
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes thats what i meant ! done !
     
    blossom belle
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:Here is a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code. So the compiler doesn't know and care) about the actual objects, the compiler only knows about the types of the reference variables.

    Here are two very simple (and hopefully easy to remember) but very, very, very important rules (which you must know by heart):

  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • Hope it helps!
    Kind regards,
    Roel

    PS. A dog extending from cat?




    i have always wondered , though i know it is the way how it works ,

    when the compiler compiles the code line by line, and sees a statement like : Animal ab = new Cat(); //  where Cat is a subclass of Animal
    why would it ignore the fact that ab reference is being assigned a subclass Cat instance (the Cat instance IS-A Animal, so it compiles fine, but why the compiler doesn't keep track of the exact kind of object that is to be created by this statement, by seeing the word Cat)

    if i would do something like : Animal ab= new Flower(); // where Animal and Flower are not involved in any inheritance ,
    the compiler would show an error since it checks that Flower is not IS-A Animal ......
    But in the case of Cat , Cat is not an actual Animal , yet the compiler knows that Cat has the IS-A Animal relation and so it compiles. In this process, why doesn't it use this information about Cat being the object , when its reference is being used to access variables and methods.


     
    blossom belle
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    is it that this is just the way the java compiler is designed ?
     
    Henry Wong
    author
    Posts: 23879
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    shambhavi sham wrote:is it that this is just the way the java compiler is designed ?



    Hate to point out the obvious, but ... the Java compiler runs during compile time...   ... And even during compilation, the compiler is really limited on visibility during compilation.

    What if, during runtime, the variable is set by another class? Or set by the subclass? Or it is set during a method call? Or it is set by another thread? The compiler doesn't have any visibility to any of this.

    Yes, for very limited cases, where the assignment and the instantiation are done at (nearly) the same time; and the variable (can be determined as) doesn't escape the method, then yes, the compiler can, in theory, know. However, what would be the advantage here? The compiler's job is complex enough. Does it need to handle a special case that is incredibly limited, at best? Or mainly for examples?

    Henry
     
    blossom belle
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay so thats how the java compiler is designed to work !
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    shambhavi sham wrote:is it that this is just the way the java compiler is designed ?


    I wonder what you expect the compiler to do if the instantiation of the appropriate animal is more complex than Animal ab = new Cat(); or depends on a parameter passed at runtime by the user? For example, how would the compiler know the exact type of the object reference variable ab is referring to
     
    blossom belle
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:

    shambhavi sham wrote:is it that this is just the way the java compiler is designed ?


    I wonder what you expect the compiler to do if the instantiation of the appropriate animal is more complex than Animal ab = new Cat(); or depends on a parameter passed at runtime by the user? For example, how would the compiler know the exact type of the object reference variable ab is referring to




    Ah!  I like this one !              thanks !!    
    so looks like there is some reason always for every rule or way of funtioning !
     
    In the renaissance, how big were the dinosaurs? Did you have tiny ads?
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
      Bookmark Topic Watch Topic
    • New Topic