Monday, April 13, 2015

C# - Abstract Class

What is an Abstract Class?
An abstract class is a special kind of class that has no implementation. It cannot be instantiated. Its implementation logic is provided by the classes that derive from it. It can have both abstract as well as non-abstract methods. It is not compulsory to have only abstract methods in an abstract class. We can also have an abstract class with only non-abstract methods.


The dictionary meaning of the word "abstract" is a thought or idea that has no physical existence and is just conceptual. We can use the idea to build something of a physical existence.

The abstract keyword can be used with classes, methods, properties, indexers and events. If we use the abstract keyword with a class, it indicates that the class is intended to be a base class and can have abstract methods (ideas) that must be implemented in a derived class (physical existence).

Why do we need an Abstract Class?
With an Abstract Class, we can provide some kind of default functionality for all derived classes to extend from. This is useful to avoid code duplication in many cases.

Abstract classes are also useful in the case of modifications to the project. If you plan on updating the base class in your project, it is better to make the class abstract. Because you can define a functionality in an abstract base class and automatically all the inheriting classes will have the same functionality without disturbing the hierarchy.

How to define an Abstract Class?
We can create an abstract class by putting the keyword abstract before a class definition as follows:
The code above defines a simple abstract class. However, we cannot create an object/instance of abstract class. It gives us an error straightaway.
So, we need to define members in it that can be in derived classes.We can define abstract as well as non-abstract members in an abstract class. An abstract class with non-abstract method is as follows:

The Experia class shows a non-abstract method call() that provides the default functionality to all sub classes that are derived from it. We cannot create an object of Experia class but we can still use the call() method in derived classes.


OUTPUT
 

The code above shows a simple inheritance of an abstract class into a concrete class. 
This type of inheritance can also be done by two concrete classes.

So, why do we want an abstract class? 
The answer is, to provide default functionality and to add abstract methods. The Experia class is inherited by all Experia models, so the call() method is required in all the models. It is better to define a call() method in the abstract class so that each derived class can have the call() method automatically and doesn't need to define it again.

Each Experia model has some of its own features like Color and Model. 
So, we can define a contract in an abstract class that must be implemented in derived classes as per their requirements. 
These types of contracts are called abstract methods and in this example is Model(). Abstract methods only have a signature and no implementation. 
It is a kind of contract that forces all the subclasses to implement it. Like the abstract class, abstract methods are also declared using the abstract keyword. It may be noted that an abstract method cannot be private or it gives an error:



So, the correct way to declare an abstract method is as follows:

If you want to override the base class method in derived class, use the override keyword with the method and if your derived class method is not related in any way with the base class method, use the new keyword. The new keyword signifies that the method in the derived class has nothing to do with the base class method.

Let's use the ExperiaZ1 class that now has methods from the abstract class as well as its own methods.

OUTPUT


Key Points

  1. We cannot create an object of Abstract Class but we can create a reference of it.
  2. An inheritance between abstract to abstract classes is possible. We don't need to implement abstract methods of the base abstract class into a derived abstract class. We can implement it later in concrete classes.
  3. An abstract class can never be sealed or static.
  4. An abstract class can have abstract as well as non abstract methods. 
  5. The abstract keyword can be used with class, methods, properties, indexers and events. 
  6. Abstract members can only be declared inside an abstract class. 
  7. An abstract member cannot be static or private. 
  8. An abstract method cannot be marked virtual. 
  9. A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible. 
  10. Without an abstract class, we cannot implement the Template Method Pattern.
references: http://www.c-sharpcorner.com



1 comment: