CSC/ECE 517 Fall 2011/ch17 5b uo: Difference between revisions
Jump to navigation
Jump to search
(Created page with "When to Use Inheritance??") |
No edit summary |
||
Line 1: | Line 1: | ||
When to | = When to use Inheritance = | ||
== Introduction == | |||
Inheritance is a useful programming concept, but it is easy to abuse this concept. Often interfaces or delegation are better options. In this wiki chapter, we will discuss the situations when to use and when not to use inheritance. | |||
Inheritance is a good choice when: | |||
<b>Your inheritance hierarchy represents an "is-a" relationship and not a "has-a" relationship. </b> | |||
<b>You can reuse code from the base classes. </b> | |||
<b>You need to apply the same class and methods to different data types.</b> | |||
<b>The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.</b> | |||
<b>You want to make global changes to derived classes by changing a base class.</b> | |||
These points are discussed in detail below | |||
== Representing "Is-a" Relationship == | |||
== Code Reuse == | |||
===<b><u>Third level title if any</u></b>=== | |||
<pre> | |||
code here if any | |||
</pre> | |||
===<b>Third Level Heading 2</b>=== | |||
<pre> | |||
Add Code here | |||
</pre> | |||
== Applying Same Class methods to different data types == | |||
== Shallow Class Hierarchies == | |||
== Global Changes to Derived Classes Through the Base Class == | |||
== Conclusion == | |||
== References == | |||
*[http://web.cs.mun.ca/~donald/bsc/node13.html http://web.cs.mun.ca/~donald/bsc/node13.html] |
Revision as of 14:53, 30 October 2011
When to use Inheritance
Introduction
Inheritance is a useful programming concept, but it is easy to abuse this concept. Often interfaces or delegation are better options. In this wiki chapter, we will discuss the situations when to use and when not 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.
These points are discussed in detail below
Representing "Is-a" Relationship
Code Reuse
Third level title if any
code here if any
Third Level Heading 2
Add Code here