C#
  Home arrow C# arrow Page 4 - Inheritance in C#
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 
Moblin 
JMSL Numerical Library 
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
     
     
    ADVERTISEMENT


    Inheritance in C# - Calling Base Class Constructors


    (Page 4 of 4 )

    In Example 11-1, the new class ListBoxderives fromWindowand has its own constructor, which takes three parameters. TheListBoxconstructor invokes the constructor of its parent by placing a colon (:) after the parameter list and then invoking the base class constructor with the keywordbase:

      public ListBox( int theTop, int theLeft, string theContents):
      
    base(theTop, theLeft) // call base constructor

    Because classes cannot inherit constructors, a derived class must implement its own constructor and can only make use of the constructor of its base class by calling it explicitly.

    If the base class has an accessible default constructor, the derived constructor is not required to invoke the base constructor explicitly; instead, the default constructor is called implicitly as the object is constructed. However, if the base class does not have a default constructor, every derived constructor must explicitly invoke one of the base class constructors using thebasekeyword. The keywordbaseidentifies the base class for the current object.

    As discussed in Chapter 7, if you do not declare a constructor of any kind, the compiler creates a default constructor for you. Whether you write it yourself or you use the one provided by the compiler, a default constructor is one that takes no parameters. Note, however, that once you do create a constructor of any kind (with or without parameters), the compiler does not create a default constructor for you.

    Controlling Access

    You can restrict the visibility of a class and its members through the use of access modifiers, such as public,private, andprotected. (See Chapter 8 for a discussion of access modifiers.)

    As you’ve seen,publicallows a member to be accessed by the member methods of other classes, whileprivate indicates that the member is visible only to member methods of its own class. Theprotectedkeyword extends visibility to methods of derived classes.

    Classes, as well as their members, can be designated with any of these accessibility levels. If a class member has a different access designation than the class, the more restricted access applies. Thus, if you define a class,myClass, as follows:

      public class MyClass
     
    {
       // ...
      
    protected int myValue;
     
    }

    the accessibility formyValueis protected even though the class itself is public. A public class is one that is visible to any other class that wishes to interact with it. If you create a new class,myOtherClass, that derives frommyClass, like this:

      public class MyClass : MyOtherClass
      {
       
    Console.WriteLine("myInt: {0}", myInt);
      }

    MyOtherClasscan accessmyInt, becauseMyOtherClassderives fromMyClass, andmyIntis protected. Any class that doesn’t derive fromMyClass would not be able to accessmyInt.

    It is more common to make properties and methods protected than it is to make member variables protected. Member variables are almost always private.

    Please check back next week for the conclusion to this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - 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...





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