• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt: .class file contents in case of inheritance

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt related to the intermediate bytecode (.class files) generated when inheritance is involved in the scene. Please consider the below code:

Dog.java


After compiling Dog.java, 2 class files are generated: Animal.class and Dog.class.
Animal.class file contains the bytecode of Animal class. Till this point, everything is fine.

Now, my question goes like this:
What are the contents of Dog.class file? Does Dog.class contains the bytecode only related to Dog class or it includes the bytecode of Animal class as well i.e. Bytecode of Animal + Bytecode of Dog? (Animal class being super class of Dog class)
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use the javap command to read the class file:

C:\slop>javap Dog.class
Compiled from "Dog.java"
public class Dog extends Animal {
Dog();
}

C:\slop>javap Animal.class
Compiled from "Dog.java"
class Animal {
Animal();
}

C:\slop>
 
Sachin Dravid
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:you can use the javap command to read the class file:

C:\slop>javap Dog.class
Compiled from "Dog.java"
public class Dog extends Animal {
Dog();
}

C:\slop>javap Animal.class
Compiled from "Dog.java"
class Animal {
Animal();
}

C:\slop>



Thanks Fred.

I was able to run 'javap' and got the following:

G:\>javap Animal.class
Compiled from "ConstructorExample_02.java"
class com.learning.java.Animal {
java.lang.String name;
java.lang.String nameOfOwner;
com.learning.java.Animal();
com.learning.java.Animal(java.lang.String);
}

G:\>javap Dog.class
Compiled from "ConstructorExample_02.java"
class com.learning.java.Dog extends com.learning.java.Animal {
java.lang.String name;
int age;
int weight;
com.learning.java.Dog();
com.learning.java.Dog(java.lang.String);
com.learning.java.Dog(java.lang.String, int);
com.learning.java.Dog(java.lang.String, int, int);
public java.lang.String getName();
}

I did not see any code related to Animal inside Dog.class file. It simply states that Dog extends Animal (2nd line in Dog.class).

So, exactly how the things happen at runtime can you please elaborate a little? Till now, I was assuming that all the permitted (non private) code related to superclass is copied into subclass bytecode, since subclass inherits those properties and methods. But, this is not the exact scene as I was assuming.

Can I assume that at runtime, Java runtime links the Animal class with Dog class somehow? And if it is true, then how?
 
Sachin Dravid
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Any inputs on the above post are awaited
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Dravid wrote:Can I assume that at runtime, Java runtime links the Animal class with Dog class somehow? And if it is true, then how?


If you really want chapter and verse, then I'm afraid you'll need to go through the JVM spec - and it's pretty dry reading, let me warn you.

The fact is that in 12 years of writing Java, I've never once had to worry about it. Java is not like C or C++, where this information is sometimes useful.

If I say A extends B, I just expect the language to do it, and I don't really care how; just as when I say new A(), I expect the JVM to create me a new object of type A. How it does it, or where it puts it in memory is really none of my concern - and in fact is more likely to be a distraction if I'm thinking about it when I program.

Sorry if it's not what you wanted to hear.

Winston
 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Sachin Dravid wrote:Can I assume that at runtime, Java runtime links the Animal class with Dog class somehow? And if it is true, then how?


If you really want chapter and verse, then I'm afraid you'll need to go through the JVM spec - and it's pretty dry reading, let me warn you.

The fact is that in 12 years of writing Java, I've never once had to worry about it. Java is not like C or C++, where this information is sometimes useful.

If I say A extends B, I just expect the language to do it, and I don't really care how; just as when I say new A(), I expect the JVM to create me a new object of type A. How it does it, or where it puts it in memory is really none of my concern - and in fact is more likely to be a distraction if I'm thinking about it when I program.

Sorry if it's not what you wanted to hear.

Winston



As Winston said that's the magical part of the JVM. We don't need to worry about pointers, garbage collection, etc, it does it all for us, which some C coders might be all complainy about, but for most of us we like automated goodness

I think this is a good question to learn from, but also like Winston said nothing we need to really worry or think about, unless the JVM messes up... Then you'll have to dig
 
reply
    Bookmark Topic Watch Topic
  • New Topic