Results 1 to 15 of 15
  1. #1
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default How is memory allocated during inheritance

    Example:

    We have an Animal class and a Dog class. Dog inherits from Animal.

    When we say the following


    Animal animal1 = new Dog(); // does the jvm allocate memory for both dog and animal object? how else would overridden methods get called if animal object was not created along with dog object during this statement?>

    Dog animal2 = new Dog(); // does jvm only allocate memory for dog class?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How is memory allocated during inheritance

    The memory is allocated for the object, based on the class.
    So a Dog object will have a place on the heap to hold member data covering the Dog class, Animal class and Object class...it's one object, not three.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How is memory allocated during inheritance

    Does any one think that knowing this kind of info about how the JVM works can be used for anything practical?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How is memory allocated during inheritance

    For one, I would think this is JVM specific. So any answer is irrelevant unless directed to a particular implementation. And what Toll's said is probably the most logical way of doing it.

    But to your question it is either idle curiosity or some teacher asked it on a test. But knowing it doesn't affect any of my code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How is memory allocated during inheritance

    Quote Originally Posted by Norm View Post
    Does any one think that knowing this kind of info about how the JVM works can be used for anything practical?
    Well, knowing that there is only one object involved is useful. Otherwise the next question is "can I get just the Animal object out?", which would be the wrong way of looking at an object in Java.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: How is memory allocated during inheritance

    I guess the part that confuses me is something like this

    Animal animal1 = new Dog();// if this is one object in memory, how does animal1 only know it can use the Animal methods and not the Dog methods?

    The way I thought about it logically is that there would be one giant object with two separate object references inside of it, one to Animal class and one to Dog class, the animal1 reference would point to that Animal reference inside the main Object.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How is memory allocated during inheritance

    There are two things in that line.
    There is the "new Dog()" part, which creates a Dog object on the heap returns a reference to it, and there is the "Animal animal1 = " which assigns that reference to the variable animal1.

    So, the compiler knows that from then on when it sees animal1 in the code, and it is in scope, it is dealing with an Animal reference...never mind what the object is being referred to.
    This is a compilation thing...not run time.
    Consequently, even though the underlying object has a bark() method, the compiler will not allow the code to compile with an animal1.bark() call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How is memory allocated during inheritance

    What if the animal1 variable is cast to Dog? The object is really a Dog so the bark method can be called with the cast.

    This is implementation dependent.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How is memory allocated during inheritance

    Then you are telling the compiler it's a Dog.
    Either way, the reference (and casting) are compile time things.
    It has no effect on the underlying (runtime) object.
    Or (apart from the addition of a CHECKCAST) on the bytecode.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How is memory allocated during inheritance

    Think of the class and all its available methods and parent classes. When you assign the class to its super class or to any interface types it has implemented, pretend that the other methods are simply greyed out. They are still there but are not accessible via that particular type. This virtual greying out of methods (making them unavailable) happens at compile time (as Toll's) said. They are still there, but now they can't be "seen" because the assigned type that is being used to access the object time may not know about them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: How is memory allocated during inheritance

    Okay that helps clarify things. Thank you all!

  12. #12
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: How is memory allocated during inheritance

    This is what for HeadFirst Java book is good for. Good explanations of these concepts.

  13. #13
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: How is memory allocated during inheritance

    In essence, in this example Animal animal1 = new Dog();

    This will create a DOG object, although the dog object has all the DOG AND ANIMAL methods/attributes(not just DOG properties), now the fact that we save it in the animal1 reference just states to the compiler that we want to ignore anything BELOW the reference of the animal1 hierarchy, so in our case, all the DOG's properties.... however if there is an overridden value in the subclass then we can call that and get the dog implementation... finally got it! but took a while for it to sink in.

  14. #14
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: How is memory allocated during inheritance

    When you say Dog doggy = new Dog(); you are making an object on the heap that is of class Dog, and a reference to it named doggy, that is meant to operate with objects of class Dog. You use the = sign to connect the reference with the object. Than means that in the future you can use the reference doggy to manipulate with newly created object on the heap. If Dog class inherits Animal class, then that doggy object on the heap will implement all that code that the Animal class instructs, and then later wrap itself around it. For example, if the Animal class has defined some instance variable which doesn't exist in the Dog class, you will be able to read it with your doggy object. There doesn't have to be any object of Animal class instantiated.


    Java Code:
    public class Animal {
        public String latinName = "animalis";
        
        public static void main(String args[]) {
             Dog doggy = new Dog();
            System.out.println(doggy.latinName);
        }
        
    }
    
    class Dog extends Animal{}
    If you say Animal doggy = new Dog(); you are still making an object on the heap that is of class Dog and a reference to it named doggy, but now this reference is meant to operate with objects of class Animal. That's why the code down will not work. You'd still have whole Dog object on the heap, and it would still envelop the Animal part inside, but this time the reference would not be able to see the doglike part of the object. It would see only the animalistic part.

    Java Code:
    public class Animal {
        public String latinName = "animalis";
        
        public static void main(String args[]) {
            Animal doggy = new Dog();
            System.out.println(doggy.estimatedLife);
        }
        
    }
    
    class Dog extends Animal{
        public int estimatedLife = 20;
    }
    How is memory allocated during inheritance-inheritance.jpg

    The same way the code down won't work, because doggy, which is Animal type reference, doesn't know about printor() method.

    Java Code:
    public class Animal {
        public String latinName = "animalis";
        
        public static void main(String args[]) {
             Animal doggy = new Dog();
             doggy.printor();
        }
        
    }
    
    class Dog extends Animal{
        public int estimatedLife = 20;
        
        public void printor(){
            System.out.println(estimatedLife);
        }
    }
    For it to work you have to insert at least blank method printor() into Animal class, which Dog class would override.
    Last edited by cronnin; 02-02-2017 at 01:04 AM.

  15. #15
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: How is memory allocated during inheritance

    Mistake. Delete this. Having extreme trouble with Firefox and this forum.
    Last edited by cronnin; 02-02-2017 at 01:02 AM.

Similar Threads

  1. Replies: 2
    Last Post: 08-20-2012, 07:38 PM
  2. HOW MUCH SPACE is ALLOCATED TO A METHOD
    By gauravmanral in forum New To Java
    Replies: 2
    Last Post: 07-15-2011, 08:14 PM
  3. get maximum heap size that can be allocated
    By gautam.aditya97 in forum Java 2D
    Replies: 3
    Last Post: 04-05-2010, 06:03 AM
  4. Replies: 2
    Last Post: 08-21-2008, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •