CHAPTER 18

image

Abstract

An abstract class provides a partial implementation that other classes can build on. When a class is declared as abstract it means that the class can contain incomplete members that must be implemented in derived classes, in addition to normal class members.

Abstract members

Any member that requires a body can be declared abstract – such as methods, properties and indexers. These members are then left unimplemented and only specify their signatures, while their bodies are replaced by semicolons.

abstract class Shape
{
  // Abstract method
  public abstract int GetArea();
  
  // Abstract property
  public abstract int area { get; set; }
  
  // Abstract indexer
  public abstract int this[int index] { get; set; }
  
  // Abstract event
  public delegate void MyDelegate();
  public abstract event MyDelegate MyEvent;
  
  // Abstract class
  public abstract class InnerShape {};
}

Abstract example

As an example, the class below has an abstract method named GetArea.

abstract class Shape
{
  private int x = 100, y = 100;
  public abstract int GetArea();
}

If a class derives from this abstract class it is then forced to override the abstract member. This is different from the virtual modifier, which specifies that the member may be overridden.

  
class Rectangle : Shape
{
  public int GetArea() { return x * y; }
}

The deriving class can be declared abstract as well, in which case it does not have to implement any of the abstract members.

abstract class Rectangle : Shape {}

An abstract class can also inherit from a non-abstract class.

class NonAbstract {}
abstract class Abstract : NonAbstract {}

If the base class has virtual members, these can be overridden as abstract to force further deriving classes to provide new implementations for them.

class MyClass
{
  void virtual Dummy() {}
}
  
abstract class Abstract : MyClass
{
  void abstract override Dummy() {}
}

An abstract class can be used as an interface to hold objects made from derived classes.

Shape s = new Rectangle();

It is not possible to instantiate an abstract class. Even so, an abstract class may have constructors that can be called from derived classes by using the base keyword.

Shape s = new Shape(); // compile-time error

Abstract classes and interfaces

Abstract classes are similar to interfaces in many ways. They can both define member signatures that deriving classes must implement, and neither one of them can be instantiated. The key differences are first that the abstract class can contain non-abstract members, while the interface cannot. And second, that a class can implement any number of interfaces but only inherit from one class, abstract or not.

// Defines default functionality and definitions
abstract class Shape
{
  public int x = 100, y = 100;
  public abstract int GetArea();
}
class Rectangle : Shape {}     // class is a Shape
  
// Defines an interface or a specific functionality
interface IComparable
{
  int CompareTo();
}
class MyClass : IComparable {} // class can be compared

An abstract class can, just as a non-abstract class, extend one base class and implement any number of interfaces. An interface, however, cannot inherit from a class. Although it can inherit from another interface, which effectively combines the two interfaces into one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.168.203