Forums Register Login

Doubt: .class file contents in case of inheritance

+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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>
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
Hello,

Any inputs on the above post are awaited
1
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1096 times.
Similar Threads
Classpaths with packages
Constructot Doubt
Doubt regarding object initialization?
constructor doubt
Type-safe generic code doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:47:08.