aspose file tools*
The moose likes Java in General and the fly likes Doubt: .class file contents in case of inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt: .class file contents in case of inheritance" Watch "Doubt: .class file contents in case of inheritance" New topic
Author

Doubt: .class file contents in case of inheritance

Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
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)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
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

Joined: Jan 21, 2013
Posts: 12
Hello,

Any inputs on the above post are awaited
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt: .class file contents in case of inheritance