This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Someone explain me on inhertance.  RSS feed

 
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In inheritance if the subclass inherits all(not final) the methods of the super class,but the methods of super class are stored in sub class object or in superclass object or in both,if both there should be unnecessary memory allocation.please explain me how inheritance works.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand your question, but I think that this other topic is talking about something similar.
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I THINK you're asking if it's a waste of space in memory when a sub class inherits the super class methods.

The code for the method is only stored in one place - and the same code is used whether it's called on a super or sub class. There is no duplication for each sub-class.
 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear,Fred YOUR THINK is correct ,but i couldn't get that "The code for the method is only stored in one place".
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but the methods of super class are stored in sub class object or in superclass object or in both,if both there should be unnecessary memory allocation.


You can test this by creating a large class A with many methods, and a class B that extends it, but defines no methods of its own. Observe the file size of both class files. What do you infer from that?

Then you'll understand what Fred was talking about.
[ February 10, 2008: Message edited by: Ulf Dittmer ]
 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer for yor suggestion .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!