Abstract class

Abstract class is a class that has no direct instances, but whose descendants may have direct instances. There are case i which it is useful to define classes for which the programmer never intends to instantiate any objects; because such classes normally are used as bae-classes in inheritance hierarchies, we call such classes abstract classes These classes cannot be used to instantiate objects; because abstract classes are incomplete. Derived classes called concrete classesmust define the missing pieces. Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts ot those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error, unless the derived class also is an abstract class.An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class. Note that an abstract class does not mean that it should contain abstract members. Even we can have an abstract class only with non abstract members

using System;
namespace abstractSample
{
//Creating an Abstract Class

abstract class absClass
{
//A Non abstract method

public int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}

//An abstract method, to be

//overridden in derived class

public abstract int MultiplyTwoNumbers(int Num1, int Num2);
}

//A Child Class of absClass

class absDerived:absClass
{
[STAThread]
static void Main(string[] args)
{
//You can create an

//instance of the derived class

absDerived calculate = new absDerived();
int added = calculate.AddTwoNumbers(10,20);
int multiplied = calculate.MultiplyTwoNumbers(10,20);
Console.WriteLine("Added : {0},
Multiplied : {1}", added, multiplied);
}

//using override keyword,

//implementing the abstract method

//MultiplyTwoNumbers

public override int MultiplyTwoNumbers(int Num1, int Num2)
{
return Num1 * Num2;
}
}
}

In the above sample, you can see that the abstract class absClass contains two methods AddTwoNumbers and MultiplyTwoNumbers. AddTwoNumbers is a non-abstract method which contains implementation and MultiplyTwoNumbers is an abstract method that does not contain implementation.

The class absDerived is derived from absClass and the MultiplyTwoNumbers is implemented on absDerived. Within the Main, an instance (calculate) of the absDerived is created, and calls AddTwoNumbers and MultiplyTwoNumbers. You can derive an abstract class from another abstract class. In that case, in the child class it is optional to make the implementation of the abstract methods of the parent class.

Abstract properties

Following is an example of implementing abstract properties in a class.

//Abstract Class with abstract properties
abstract class absClass
{
protected int myNumber;
public abstract int numbers
{
get;
set;
}
}

class absDerived:absClass
{
//Implementing abstract properties

public override int numbers
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
}

Important rules applied to abstract classes

  • An abstract class cannot be a sealed class
  • An abstract method cannot be private.
  • The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
  • An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
  • An abstract member cannot be static
  • Virtual method has an implementation & provide the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method
  • A class can inherit one or more interfaces, but only one abstract class
Abstract class vs. Interface
  • An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class
  • The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.
  • A class can inherit one or more interfaces, but only one abstract class.
  • No fields can be defined in interfaces.An abstract class can have fields and constrants defined
  • Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

0 comments:

Template by - Mathew | Mux99