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 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.
|
|