• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Inheritence

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In inheritence what exactly you mean by substitution and delegation?
I guess substitution is where the parent object can be replaced with any child object.
Delegation is the object delegation from child to parent.
Let me know if I am right.
yup
- Raj
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, substitution means anyplace you expect a parent, you should be able to use any child. IE if I have an argument of type Parent and somebody passes me a Child I should still work just fine.
This is not as easy as it sounds, and leads to some interesting paradoxes. One classic example is Square as subclass of Rectangle that happens to have identical height & width. Say Square overrides setHeight to also setWidth (if not already equal) and setWidth also calls setHeight. That keeps the sides equal. Passes the "a square is a rectangle" test in English, but can you substiute Square anywhere you expect Rectangle?

Ouch! A Square won't fit in a Rectangular hole!
 
Raj Waters
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you expect a square, the substitution works just fine. But if you expect a Rectangle do not substitute. Very simple. Coz the parent class and child class both has independent behaviors.
Now when substituting parent with child, does it mean delegation?
yup
- Raj
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is delegation exactly related to inheritance
I can see delegation in different contexts like delegating some of the advertised behaviour to the contained object internally; delegating the responsibility in a chain, etc.
Thanks.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good take on delegation. A number of patterns like Chain of Command use it. It's also a good way around the lack of multiple-inheritance. If you have several classes with some common behavior but they are not a derived from a common ancestor they can all delegate the work to a common helper.
Back to substitution - the rule is rather formal. From a
Robert Martin Engineering Notebook article:


The Liskov Substitution Principle
FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT .
The above is a paraphrase of the Liskov Substitution Principle (LSP). Barbara Liskov first wrote it as follows nearly 8 years ago 1 :
What is wanted here is something like the following substitution property: If for each object o 1 of type S there is an object o 2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o 1 is substituted for o 2 then S is a subtype of T.
The importance of this principle becomes obvious when you consider the consequences of violating it. If there is a function which does not conform to the LSP, then that function uses a pointer or reference to a base class, but must know about all the derivatives of that base class. Such a function violates the Open-Closed principle because it must be modified whenever a new derivative of the base class is created.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic