Monday, 29 January 2018

What are Abstract Classes? A Simple Explanation

In this lesson we would cover the following:
  1. What are Abstract Classes?
  2. What are Abstract Methods?
  3. When can an Abstract class be used?
  4. Abstract class implementation example
  5. Interview Questions
    • Can we create an instance of an abstract class?
    • Can an abstract class include a constructor
    • Can an abstract class inherit another class?
    • Can an abstract class inherit an interface?(What is an interface anyway?)


What is An abstract class
An abstract class is a class that have been defined using the abstract keyword.
The abstract keyword is used to indicate that the class or method has an incomplete implementation. This means another class, a subclass of the abstract class has to provide the missing implementation of the abstract class.
According to the C# reference,  'the abstract modifier can be used with classes, methods, properties, indexers, and events'.
If abstract is applied to a class, then the class cannot be instantiated. This means that you cannot directly create a new object of an abstract class. To use an abstract class, you need to create a subclass that derives the abstract class.

What is an Abstract Method?
An abstract method is a method that have be declared using the abstract keyword. If a method is marked as abstract, then any subclass must implement this method. If a subclass fails to implement an abstract method, then the program would report a compile-time error.
Also, if a method is marked as abstract, then the class is automatically abstract. If a class contains an abstract method, but is not marked as abstract, then the code would not compile. However, an abstract class can have a non-abstract method inside.

Let's take an example
Consider the class vehicle. The class vehicle has a method called Move(). So we can create this class as an abstract class and then create subclasses such as Car, Train and Motorbike.
   
public abstract class Vehicle
    {
 public String Name;
//Abstract Method, must be implemented in sub-classes

public abstract String Move();
    }

    public class Car: Vehicle
    {
        String name;
        public override string Move()
        {
            return "I move on Land";
        }
    }

    public class Train: Vehicle
    {
        String name;
        public override string Move()
        {
            return "I move on Rail road";
        }
    }

In the above example, the Vehicle class have been declared as abstract and also contains an abstract method Move().
The tree subclasses: Car, Boat and Train implements this Move() method. Note the keyword override used before the Move method in the subclasses. Now, this keyword is optional. Without using the keyword, the program recognized that the method is being overriden since it is marked as abstract in the base class and have the same name in the subclass.
But if a method is not marked as abstract, the you can used the override keyword to force an override in the subclasses.

When do we Need and Abstract Class
An abstract class is needed when the object could be implemented in different ways. An example is a class that represents shapes. It could be implemented as a Triangle, Rectangle, Square etc. In this case, a class defining Shape needs to be an abstract class. Another example as illustrated in our code is the Vehicle class.


Interview Questions
  • Can we create an instance of an abstract class?  No
  • Can an abstract class include a constructor? Yes
  • Can an abstract class inherit another class? Yes
  • Can an abstract class inherit an interface?(What is an interface anyway?) Yes (Interfaces are explained in another lesson)