File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt: .class file contents in case of inheritance

 
Sachin Dravid
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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)
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Any inputs on the above post are awaited
 
Winston Gutkowski
Bartender
Pie
Posts: 9484
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • 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
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic