Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java--inheritance,reusability,object oriented design

 
Shishir Shivhare
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying inheritance.and i have some confusion in it.
Suppose if a class inherit another class.Will the child class will write the code of its parent class in it when it is converted at machine level or just link it.
What is meaning of reuse of code.Is it copying of code?
If code is reused, i think it will increase coupling then why it is beneficial for object oriented design where low coupling is required?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shishir Shivhare wrote:
Suppose if a class inherit another class.Will the child class will write the code of its parent class in it when it is converted at machine level or just link it.


There is only one copy of the code.

What is meaning of reuse of code.Is it copying of code?
If code is reused, i think it will increase coupling then why it is beneficial for object oriented design where low coupling is required?


Code reuse means not copying code. It's when you write code once, and use that method, class, or library in multiple situations. And note that the main use of inheritance is not code reuse. It is type implementation and specialization. Code reuse comes along as a natural side effect, when the parent class implements a method in a way that is appropriate for the child class as well. Again, though, we don't inherit from a class just to use its methods. If that's all we want--that is, if we're not making a specialized type of the parent class--then we should just use composition instead of inheritance.
 
Shishir Shivhare
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will inheritance increase coupling??If yes then it may effect object oriented design?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shishir Shivhare wrote:Will inheritance increase coupling??


To some extent, yes, but when used appropriately, the coupling is a natural part of the reason for the inheritance in the first place, and is a negligible cost compared to the benefit garnered by type specialization, abstraction, and polymorphism.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic