Here is a personal example where I've made use of inheritance to greatly benefit a development situation:
I needed to develop a asp.net (c#) form control set, which would allow both standard web forms (bunch of form fields with submit button), as well as a secure version which talks to a web service to submit the information over SSL.
Because of all of the similarities between the controls and concepts, I developed a number of classes:
BaseWebControl
- custom base control class that inherits from
System.Web.UI.WebControl
class (part of .NET framework
- has custom properties and methods that are used in our application by all custom controls (control state info, etc.)
BaseFormControl
- inherits from BaseWebControl, gaining all of its underlying functionality
- handles base form functionality, such as dynamically adding fieldsets, marking required fields, adding submit button, etc. etc.
- contains a label and associated control index for easy lookups
- marked as an abstract class, with abstract method called
SubmitForm
. This method is not defined on this class, however it is called by the submit button click event. This means that any specific form control class that inherits from this base class can implement the abstract SubmitForm
functionality as needed.
EmailFormControl
- Inherits from
BaseFormControl
, so it gains all underlying functionality above without any duplication
- contains very little, except overrides the abstract method
SubmitForm
, and generates an email based on the form fields.
- all other control functionality and event handling is dealt with by the base class. In the base class when the submit button is clicked and handled, it calls this specific implementation of
SubmitForm
SecureFormControl
- Again inherits from
BaseFormControl
, so it gains all underlying functionality above without any duplication
- In its implementation of
SubmitForm
, it connects to a WCF web service and passes the information in over SSL.
- no other functionality is required because base class handles the rest.
In stripped down code form, the general outline is as such:
public class BaseWebControl : System.Web.UI.WebControl
{
//base web control with application wide functionality built in
}
public abstract class BaseFormControl : BaseWebControl
{
//handles all 'common' form functionality
//...
//...
//event handler for submit button calls abstract method submit form,
//which must be implemented by each inheriting class
protected void btnSubmit_Click(object sender, EventArgs e)
{
SubmitForm();
}
protected abstract SubmitForm();
}
public class EmailFormControl : BaseFormControl
{
protected override SubmitForm()
{
//implement specific functionality to email form contents
}
}
public class SecureFormControl : BaseFormControl
{
protected override SubmitForm()
{
//connect to WCF web service and submit contents
}
}
As a result of the above, BaseFormControl
has about 1000 lines of code in a whole bunch of methods, properties, etc. SecureFormControl
and EmailFormControl
each have about 40 lines. All other functionality is shared and controlled by the base class. This promotes:
- maintainability
- efficiency
- flexibility
- consistency
Now I can create any type of web form, such as DataBaseFormControl
, etc. etc. very easily. I can add great new functionality to all forms by adding methods and properties to the base classes, etc.
And the list goes on.
Phew that was a lot of typing. Hope this helps give you a good example. This was one instance where I found inheritance to be a key success point in a project.