7.9 Abstract methods


7.9 Abstract methods

Abstract methods in C# are pretty much the same as abstract methods in Java, except for some additional rules. Here is an example.

 1: abstract class A{ 2:   protected  abstract  int ProcessInt(int i); 3: } 4: 5: class B:A{ 6:   protected  override  int ProcessInt(int i){ 7:     return i*2; 8:   } 9: } 

Like Java

  • No implementation of that method is provided “ overriding methods of non-abstract subclasses are expected to provide the method implementation.

  • Abstract methods cannot be private. [10]

    [10] Abstract private methods don't make sense at all since abstract methods are intended to be overridden, and private methods cannot be overridden in subclasses.

  • Classes with one or more abstract methods must be declared as an abstract class (although abstract classes need not contain any abstract methods at all).

Unlike Java

  • Remember to use the override keyword [11] when overriding the abstract method in the subclass. (If you omit the override keyword on line 6, you will be performing name hiding , not method overriding. The code still compiles, but a warning is given.)

    [11] An overriding method in C# must be declared with the override keyword. See section 7.10.

  • Java allows you to change the access modifier in the overridden method to be of weaker (less strict) access. In C# you are not allowed to change the access modifier of the abstract or virtual method in the overridden method. In the previous code example, if you change line 6 to:

     6:  public  override int ProcessInt(int i){ 

    you will get a compiler error:

     test.cs(6,23): error CS0507: 'B.ProcessInt(int)': cannot change access modifiers when overriding 'protected' inherited member 'A.ProcessInt(int)' 

Additional notes

  • Abstract methods are implicitly virtual, but you cannot use the virtual modifier when declaring abstract methods (you will get a compilation error).

  • An abstract method declaration can override a method which is already implemented in the superclass. In this way, an abstract class can 'force' a reimplementation of that method in a subclass. Examine the example below.

     1: abstract class A{ 2:   protected abstract int ProcessInt(int i); 3: } 4: 5: class B:A{ // class B extends A 6:   protected override int ProcessInt(int i){ 7:     return i*2; 8:   } 9: } 10: 11: abstract class C:B{ // class C extends B 12:   protected  abstract override  int ProcessInt(int i); 13: } 14: 15: class D:C{ // class D extends C 16:   protected override int ProcessInt(int i){ 17:     return i*3; 18:   } 19: } 

    In this case, class C contains another method which is abstract, yet overrides the implementation in B . This forces non-abstract class D to contain an implementation for that method.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net