• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

alternative to inheritance

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can some one you explain Delegation, which is alternative to inheritance with example?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Wikipedia - Delegation Pattern, which includes a Java example.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is delegation really an alternative to inheritance?
Because the simple Java program mentioned in the above link illustrates a HAS-A relationship whereas inheritance is an IS-A relationship.
Please guide.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit Ghorpade:
...Is delegation really an alternative to inheritance? Because the simple Java program mentioned in the above link illustrates a HAS-A relationship whereas inheritance is an IS-A relationship...


You're right that delegation uses composition (HAS-A). But I would still say that delegation is an alternative to inheritance -- just as composition is an alternative to inheritance. This does not mean they can/should be used interchangeably. Depending on the situation, there are significant pros and cons in choosing one over another.

As an illustration, suppose I have a class with a doStuff method, and I want another class with this same behavior...

In both cases, I end up with a class where I can call doStuff and invoke the desired behavior. But as you can see, these alternatives result in very different objects.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Posted by Marc Weber

In both cases, I end up with a class where I can call doStuff and invoke the desired behavior. But as you can see, these alternatives result in very different objects.


the explanation above is just fine, and I am aware that you can invoke the required method in both ways. I was just talking about the OOP aspect.
Because in OOP, the above mentioned ways still remain entirely different.

But anyways thanks for the explanation.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic