C#
  Home arrow C# arrow Page 3 - Inheritance in C#
Click Here
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Inheritance in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2007-05-10

    Table of Contents:
  • Inheritance in C#
  • The Unified Modeling Language
  • Inheritance
  • Calling Base Class Constructors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Inheritance in C# - Inheritance


    (Page 3 of 4 )

     

    In C#, the specialization relationship is implemented using a principle called inheritance. This is not the only way to implement specialization, but it is the most common and most natural way to implement this relationship.

    Saying thatListBoxinherits from (or derives from)Windowindicates that it specializesWindow.Windowis referred to as the base class, andListBoxis referred to as the derived class. That is,ListBoxderives its characteristics and behaviors from Window and then specializes to its own particular needs.

    You’ll often see the immediate base class referred to as the parent class, and the derived class referred to as the child class, while the topmost class,Object, is called the root class.

    Implementing Inheritance

    In C#, you create a derived class by adding a colon after the name of the derived class, followed by the name of the base class: 

      public class ListBox : Window

    This code declares a new class,ListBox, that derives fromWindow. You can read the colon as “derives from.”

    The derived class inherits all the members of the base class (both member variables and methods), and methods of the derived class have access to all the public and protected members of the base class. The derived class is free to implement its own version of a base class method. This is called hiding the base class method and is accomplished by marking the method with the keywordnew. (Many C# programmers advise never hiding base class methods as it is unreliable, hard to maintain, and confusing.)

    This is a different use of the keywordnewthan you’ve seen earlier in this book. In Chapter 7,newwas used to create an object on the heap; here,newis used to replace the base class method. Programmers say the keywordnewis overloaded, which means that the word has more than one meaning or use.

    Thenewkeyword indicates that the derived class has intentionally hidden and replaced the base class method, as shown in the Example 11-1. (Thenew keyword is also discussed in the section “Versioning with new and override,” later in this chapter.)

    Example 11-1. Deriving a new class

    using System;

    public class Window
    {
       // constructor takes two integers to
       // fix location on the console
       public Window( int top, int left )
       {
         
    this.top = top;
          this.left = left;
       }

       // simulates drawing the window
       public void DrawWindow()
       {
         
    Console.WriteLine( "Drawing Window at {0}, {1}",
          top, left );
       }

       // these members are private and thus invisible
       // to derived class methods; we'll examine this
       // later in the chapter
       private int top;
       private int left;
    }

    // ListBox derives from Window
    public class ListBox : Window
    {
      
    // constructor adds a parameter
       public ListBox( int top, int left, string theContents ) :
       base( top, left ) // call base constructor
       {
         
    mListBoxContents = theContents;
       }

       // a new version (note keyword) because in the
       // derived method we change the behavior
       public new void DrawWindow()

       {
         
    base.DrawWindow(); // invoke the base method
         
    Console.WriteLine( "Writing string to the listbox: {0}",
         
    mListBoxContents );
      
    }
      
    private string mListBoxContents; // new member variable
    }

    public class Tester
    {
      
    public static void Main()
      
    {
         
    // create a base instance
         
    Window w = new Window( 5, 10 );
         
    w.DrawWindow();

          // create a derived instance
         
    ListBox lb = new ListBox( 20, 30, "Hello world" );
         
    lb.DrawWindow();
      
    }
    }

    The output looks like this:

      Drawing Window at 5, 10
     
    Drawing Window at 20, 30
     
    Writing string to the listbox: Hello world

    Example 11-1 starts with the declaration of the base classWindow. This class implements a constructor and a simpleDrawWindow()method. There are two private member variables,topandleft. The program is analyzed in detail in the following sections.

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning C# 2005, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter 11 of Learning C# 2005, Second Edition, written by Jesse Liberty and Brian MacDonald (O'Reilly, 2006; ISBN: 0596102097). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway