Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is it a good coding practice to use inheritance of Interfaces.

the IBird interface has many bird behaviours defined along with the Fly() behaviour. Now, if a Bird class (say, Ostrich) implements this interface, it has to implement the Fly() behaviour unnecessarily (Ostrich doesn't fly).

The IBird Interface is broken down into two different interfaces, IBird and IFlyingBird, where the IFlyingBird inherits IBird.

Is it a good coding practice to do like this.
Posted
Comments
Pheonyx 23-Jul-13 3:09am
   
Personally, i would say yes, break it down into two interfaces. The behaviors defined in IBird should be common across all different types of bird, where as IFlyingBird should just be behaviors that relate to Flying Birds. You could also have ISwimmingBird for behaviours that relate to swimming (for example a Duck class, or a Swan class).
lukeer 23-Jul-13 3:49am
   
And baby penguins! Does anyone ever care about the baby penguins?
Pheonyx 23-Jul-13 3:57am
   
omg! I can't believe I forgot the baby penguins! :'(
Joezer BH 23-Jul-13 4:02am
   
And what about the Dodos?
Oh... they are extinct :sigh:


1 solution

Your question is being referenced specifically in this great article:
How I explained OOD to my wife[^]

See the sction "Topic: Liskov's Substitution Principle":
Quote:
Here, the KingFisher class extends the Bird base class and hence inherits the Fly() method, which is pretty good.

Now take a look at the following example:

Corrected class hierarchy of Liskov Substitution Principle
Ostrich is a Bird (definitely it is!) and hence it inherits the Bird class. Now, can it fly? No! Here, the design violates the LSP. ...


Cheers,
Edo
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900