Polymorphism and Abstraction
To maintain a privileged relationship with its children, a parent class can make a member available only to classes derived from it. With this…
Characteristics of Inheritance#
Protected Members#
To maintain a privileged relationship with its children, a parent class can make a member available only to classes derived from it. With this relationship, some members of a parent class have a protected access level. Of course, as the class creator, it remains your job to specify this relationship. To create a member that derived classes only can access, type the protected keyword to its left. Here is an example:
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 GetFullName()
{
return _name;
}
}You may access protected members only in derived classes. Therefore, if you instantiate a class outside, you can call only public members. 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. Here are examples: Source File: Persons.cs
using System;
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 internal string FullName()
{
return _name; }
}
protected internal string Gender()
{
return _gdr;
}
}Virtual Members#
We mentioned that you can create a new version of a member in a derived class for a member that already exists in the parent class. After doing this, when you call that member in your program, you need to make sure that the right member gets called, the member in the base class or the equivalent member in the derived class. When you create a base class, if you anticipate that a certain method would need to be redefined in the derived class, you can indicate this to the compiler. On the other hand, while creating your classes, if you find out that you are customizing a method that already exists in the base class, you should let the compiler know that you are providing a new version. In both cases, the common member should be created as virtual. To create a virtual member, in the base class, type the virtual keyword to the left of the method. Based on this, the Area property of our Circle class can be created as follows:
class Circle
{
public virtual double Area()
{
return Radius * Radius * 3.14159;
}
}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:
class Sphere : Circle
{
public override double Area()
{
return 4 * Radius * Radius * 3.14159;
}
public double Volume()
{
return 4 * 3.14159 * Radius * Radius * Radius;
}
}In the same way, when implementing an abstract method of a class, type the override keyword to its left.
Abstract Classes#
In a program, 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. Therefore, an abstract class can be created only to serve as a parent class for other classes. To create an abstract class, type the abstract keyword to the left of its name. Here is an example:
abstract class Ball
{
protected int TypeOfSport;
protected string Dimensions;
}Abstract Methods#
When creating a class that would mainly be used as a base for future inheritance, you can create one or more methods and make them abstract. To do this, when creating the method, type the abstract keyword to its left. 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:
public abstract class Ball
{
protected int TypeOfSport;
protected string Dimensions;
public abstract CalculateArea();
}In the same way, you can create as many properties and methods as you see fit. You can choose what properties and methods to make abstract. This is important for inheritance.
Sealed Classes#
Any of the classes we have used so far in our lessons can be inherited from. If you create a certain class and don't want anybody to derive another class from it, you can mark it as sealed. In other words, 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 other than everything we have seen so far. Simply remember that no class can be derived from it.
Interfaces#
Introduction#
Imagine you start creating a class and, while implementing or testing it, you find out that this particular class can be used instead as a general base that other classes can be derived from. An interface is a special class whose purpose is to serve as a template that actual classes can be based on. An interface is primarily created like a class: it has a name, a body and can have members. 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
{
}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:
public class SportBall : ISportType
{
int players;
string sport;
}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:
public interface ISportType : IBall
{
}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:
public interface ISportType : IBall, ICourtDimensions
{
}You may 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:
public interface Sports: Player, IBall, ICourtDimensions
{
}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 methods. If you derive a class from an interface, you must implement all methods that were created in the interface. This means that you must define them so that, when a variable is declared of that class, the methods perform their actions. If you derive a class from an interface that itself was derived from another interface, in your class, you must define all methods that were created in the whole parental lineage.
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. As we have seen so far, in C#, you cannot simply and only declare a method in a file for a forward (later) implementation, as it's done in C, C++, C++/CLI, and (Object) Pascal. In C#, to create a class in various classes, start the class in one file but precede the class keyword with partial. Here is an example: Source File: geometry1.cs
partial class Cylinder
{
private double rad;
private double hgt;
public Cylinder(double radius, double height)
{
this.rad = radius;
this.hgt = height;
}
}After creating the class in one file, you can use as any of the classes as we have done so far. 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. One of the rules you must observe is that the partial class must have been marked as partial, as we did above. One of the advantages of partial implementation is that you don't have to get back to the first or previous file to modify it in order to complement the class. 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. Here is an example: Source File: geometry2.cs
using System;
partial class Cylinder
{
public Cylinder()
{
this.rad = 0.00;
this.hgt = 0.00;
}
public double Volume()
{
return rad * rad * hgt * Math.PI;
}
}Once a partial class has been created, you can create another based on it. The child class doesn't have to be partial, although it can be. Previous Copyright © 2008-2009 C# Key Next

