• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Inheritence vs Delegation

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
can anyone please say me the difference between the inheritence and delegation in java? Thanks in Advance.

-Thanks & Regards,
Hamsa
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hamsagayathri,

inheritance and delegation are both ways to add some functionality to a class.

The major difference is that your class is more tightly coupled to a base class if you use inheritance. Then your extending class belongs to the class hierarchy forever. This can be good or not so good depending on the situation.

Delegation means that your class is just using the functionality of one or more other classes to provide this functionality for its client classes. Therefore it delegates all work that isn't directly part of your class to other classes. The big advantage is that you can exchange the classes you are delegating to at runtime for example to provide different implementations of the delegation classes.

I hope now it's more or less clearer

Marco
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic