Monday, 29 January 2018

What are Abstract Classes? A Simple Explanation

Hello, good to see you here! Its good to know that you want to learn Java and be sure that you would find all you need to know here!

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?) 
  6. Final Notes

1. What is An abstract class


An abstract class is a class that has been defined using the abstract keyword.
The abstract keyword is used to indicate that the class or method has an incomplete implementation. This means that 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 keyword 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.

2. What is an Abstract Method?


An abstract method is a method that has been 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.



3. 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 Move(), MUST BE IMPLEMENTED IN THE SUBCLASSES
    public abstract String Move();
}

public class Car : Vehicle
{
    String name;
    public override string Move() //IMPLEMENTATION OF ABSTRACT METHOD
    {
        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 overidden 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.



4. 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.

5. 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)

6. Final Notes


I hope you now clearly understand the concept of abstract classes. If you need more clarifications  or you have any challenges following this lesson, please leave a comment to describe any issue you have.

You could take a series of Free Java Tutorials with Examples. Thank you for learning!😃😃😃😃