Home

Polymorphism and Abstraction

 

Characteristics of Inheritance

 

Namespaces and Inheritance

To derive a class from another that belongs to another class, type the name of the namespace, followed by the period operator ".", and followed by the name of the base namespace. To access a member of the parent class, remember to qualify its name with the period operator. Alternatively, to use the contents of a namespace, prior to calling a member of that namespace, use the Import attribute in the Page tag.

 

Protected Members

To maintain a privileged relationship with its children, a parent class can make a member available only to classes derived from it. To create a member that derived classes only can access, type the protected keyword to its left. Here are examples:

<script runat="server">
public class Person
{
	private string _name;
	private string _gdr;

	public Person()
	{
		this._name = "Not Available";
		this._gdr    = "Unknown";
	}
	public Person(string name, string gender)
	{
		this._name = name;
		this._gdr  = gender;
	}

	protected string FullName
	{
		get { return _name; }
		set { _name = value; }
	}

	protected string Gender
	{
		get { return _gdr; }
		set { _gdr = value; }
	}
}
</script>

You can access protected members only in derived classes. If you create a class member and mark it as protected, the classes derived of its parent class, created in the current program or outside the current program, can access it. If you want the member to be accessed only by derived classes implemented in the same program but not derived classes implemented outside of the current program, mark the member as protected internal.

Virtual Members

When you create a base class, if you anticipate that a certain property or method would need to be redefined in the derived class, you can indicate it as virtual.

To create a virtual member, in the base class, type the virtual keyword to the left of the property or method. Based on this, the Area property of our Circle class can be created as follows:

<script runat="server">
class Circle
{
    public virtual double Area
    {
	get
	{
		return Radius * Radius * 3.14159;
	}
    }
}
</script>

When you derive a class from an abstract class, since the methods (if any) of the abstract class were not implemented, you must implement each one of them in the derived class. When customizing virtual members in a derived class, to indicate that a member is already virtual in the base class and that you are defining a new version, type the override keyword to the left of its declaration. For example, the Area property in our Sphere class can be created as follows:

<script runat="server">
class Sphere : Circle
{
    public override double Area
    {
	get
	{
	    return 4 * Radius * Radius * 3.14159;
	}
    }

    public double Volume
    {
	get
	{
	    return 4 * 3.14159 * Radius * Radius * Radius;
	}
    }
}
</script>

In the same way, when implementing an abstract method of a class, type the override keyword to its left.

Abstract Classes

You can create a class whose role is only meant to provide fundamental characteristics for other classes. This type of class cannot be used to declare a variable. Such a class is referred to as abstract.

To create an abstract class, type the abstract keyword to the left of its name. Here is an example:

<script runat="server">
abstract class Ball
{
	protected int TypeOfSport;
	protected string Dimensions;
}
</script>

Abstract Properties and Methods

When creating an abstract class, you can create one or more properties and make them abstract. To do this, when creating the property, type the abstract keyword to its left. Because you would not define the property, you can simply type the get keyword and its semi-colon in the body of the property.

A method of a class also can be made abstract. An abstract method can be a member of only an abstract class. If you make a method abstract in a class, you must not implement the method.

To create an abstract method, when creating its class, type the abstract keyword to the left of the method's name. End the declaration with a semi-colon and no body for the method since you cannot implement it. Here is an example:

<script runat="server">
public abstract class Ball
{
    protected int TypeOfSport;
    protected string Dimensions;

    public abstract CalculateArea();
}
</script>

In the same way, you can create as many properties and methods as you see fit.

 

 
 
 

Sealed Classes

A sealed class is one that cannot serve as base for another class. To mark a class as sealed, type the sealed keyword to the left of the class keyword. Here is an example:

public sealed class Ball
{
	public int TypeOfSport;
	public string Dimensions;
}

There is not much to do about a sealed class. Simply remember that no class can be derived from it.

 

 
 

Interfaces

 

Introduction

An interface is a special class whose purpose is to serve as a template that actual classes can be based on. To create an interface, instead of the class keyword, you use the interface keyword. By convention, the name of an interface starts with I. Here is an example:

interface ICourtDimensions
{
}

The Members of an Interface

In an interface, you cannot declare fields like those we have used in other classes. Instead, if you want some type of member variable, you can create a property. If you create a property in an interface, you cannot define that property. One of the rules of an interface is that you cannot define any of its members. This is also valid for its properties. Therefore, if you create a property in an interface:

  • You can indicate that it would be read-only by adding an empty getter property to it. Here is an example:
     
    public interface ICourtDimensions
    {
    	double Length { get; }
    }
  • You can indicate that it would be write-only by adding an empty setter property to it. Here is an example:
     
    public interface ICourtDimensions
    {
    	double Length { set; }
    }
  • You can indicate that it would be used to write values to it and to read values from it. To provide this information, add a getter and a setter accessories to it. Here is an example:
     
    public interface ICourtDimensions
    {
    	double Length { get; set; }
    }

In the same way, you can create as many properties as you judge necessary in an interface. Besides the properties, an interface can also have other types of members such as methods.

An Interface as a Base Class

An interface is used to lay a foundation for other classes. For this reason, it is the prime candidate for class derivation. To derive from an interface, use the same technique we have applied in inheritance so far. Here is an example of a class named SportBall that derives from an interface named ISportType:

<script runat="server">
public class SportBall : ISportType
{
    int players;
    string sport;
}
</script>

Just as you can derive a class from an interface, you can create an interface that itself is based on another interface. Here is an example:

<script runat="server">
public interface ISportType : IBall
{
    SportCategory Type
    {
	get;
    }
}
</script>

The C# language doesn't allow multiple inheritance, which is the ability to create a class based on more than one class. Multiple inheritance is allowed only if the bases are interfaces. To create multiple inheritance, separate the names of interface, with a comma. Here is an example:

<script runat="server">
public interface ISportType : IBall, ICourtDimensions
{
	SportCategory Type
	{
		get;
	}
}
</script>

You can also involve a class as parent in a multiple inheritance scenario but there must be only one class. Here is an example in which a class called Sports derives from one class and various interfaces:

<script runat="server">
public interface Sports: Player, IBall, ICourtDimensions
{
}
</script>

Implementation of Derived Classes of an Interface

After creating an interface, you can derive other interfaces or other classes from it. If you are deriving other interfaces from an interface, you can just proceed as you see fit. For example, you can add or not add one or more new properties and you can add or not add one or more methods.

If you derive a class, from an interface, you must implement all properties that were created in the interface.

Class Partial Implementation

In all of the classes we have defined so far, we were using a single file to implement the class. In C#, you can create a class (the same class) in different files. This means that you can start a class in one file and continue it in another file or in other files. This is referred to as partial implementation.

To create a class in various files, start the class in one file but precede the class keyword with partial.

If you had created a partial class, or you got a partial class from somebody (not as part of a DLL or nor from another type of library), and you find out that the class is not complete, you can then complement it. You can simply start another file and continue the class in it. Two other rules you must observe are that you must use the same name for the class and you must precede the class keyword with partial.

 

 

   
 

Previous Copyright © 2009-2016, FunctionX, Inc. Next