Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 1, 2009

What is Inheritance?

Filed under: C#,OOP,VB.NET @ 10:58 am

In object-oriented (OO) terms, inheritance defines an “is a” relationship between two or more classes. A beagle is a dog, and a poodle is a dog, so both beagle and poodle inherit from dog. Both beagle and poodle have dog attributes and exhibit dog behaviors. A dog class in this example is called the parent class or base class, and the classes that inherit from it (beagle and poodle) are called child classes or derived classes.

[To begin with an overview of OO, start here.]

Likewise, think about your phone. All types of phones have basic
phone attributes and behaviors, such as volume, dial, answer, and disconnect.
Your desk phone may have additional, specialized behaviors, such
as transfer features. Your cell phone has amazing features such as taking
pictures and playing movies.

Even though each type of phone may have specialized behaviors, your
desk phone is a phone, and your cell phone is a phone, so both desk phone
and cell phone inherit their basic functionality from phone. Phone is the
parent (or base) class, and desk phone and cell phone are the child (or
derived) classes. You could draw this relationship as shown below.

image

When you inherit from a class, all the properties and methods in the
base class are available to the derived class, just as if the properties and
methods were in the derived class. So the desk phone can perform the
answer method, as can the cell phone, even though the implementation
for answer is defined in the base phone class.

Any class in the .NET Framework that is not intentionally sealed
(marked as not inheritable) can be a used as a base class. So you can inherit
the functionality of the .NET Framework classes in your application.
You can also use any class in your application as a base class and inherit
from it.

You can see inheritance in action as soon as you create your first form.
When you create a form, Visual Studio generates the following code:

In C# (in the TimeSheetWin.cs file):

public partial class TimeSheetWin: Form

In VB (in the TimeSheetWin.Designer.vb file):

Partial Class TimeSheetWin
    Inherits Windows.Forms.Form

In VB, the Inherits keyword specifies that your form inherits from the .NET
Framework’s Windows.Forms.Form class. In C#, a colon separates the class name from the base class, which again is the Form class. This Form class provides all the common code required to make your form work like a Windows form.

NOTE: In VB, the designer file is, by default, hidden from view in Solution Explorer. To see the designer files for a VB project, select the project in Solution Explorer and then click on the Show All Files button in the Solution Explorer toolbar. Repeat the process to hide the files.

As you define the classes for your application, you may find that some
classes have a number of the same properties and methods but also have
some properties and methods that are different. You can extract the common
properties and methods into another class and then use that class as
a base class.

For example, you may find that each of your business object classes
(such as Employee and TimeSheet) requires a property to keep track of
the dirty state (added, updated, deleted). You can build a business object
base class that contains this property instead of adding it to every class.
Every business object class can then inherit from this base class and therefore have this property.

[For a code example of a business object base class, see this link.]

Using inheritance can minimize the amount of repeated code in your
application. Common code is written only once in the base class and is
reused by every derived class. Inheritance also makes your derived classes
easier to build and maintain, because the derived classes focus exclusively
on the features that make them unique. And if you ever need to change
that common code, you have to change it in only one place.

(Based on an except from "Doing Objects in Visual Basic 2005".)

Enjoy!

1 Comment

  1.   Jera — June 29, 2011 @ 7:57 pm    Reply

    That’s the best asnewr of all time! JMHO

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs