21 November 2011

When to Use Inheritance ?


When to Use Inheritance ?



Inheritance is a good choice when:
  • Your inheritance hierarchy represents an "is-a" relationship and not a "has-a" relationship.
  • You can reuse code from the base classes.
  • You need to apply the same class and methods to different data types.
  • The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
  • You want to make global changes to derived classes by changing a base class.
  • Composition – Composition is used when the two class has-a relationship among classes
    Inheritance – Inheritance is used when the derived class is-a relationship base class

    For example :
    Take two class
    1.House owner
    2.House

    House owner is a House is not valid - Inheritance
    House owner has a house is valid - Composition

    For example :
    Take two class
    1.Game
    2.Cricket


    Cricket is a game is valid - Inheritance
    Cricket has a game is invalid - Composition

No comments:

Post a Comment

Comments Welcome

Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...