Constructor in an Abstract Class in Java !?

Constructor in an Abstract Class in Java !?

Let's discuss it...

Welcome to my blog, everyone!!

Today, we will discuss whether these abstract classes have a constructor or not. So, first, let me give you a brief intro to Abstract Class in java.

What is Abstract Class?

Those classes which we declare using the abstract keyword are termed Abstract Classes in java. They can have static, non-static, final and non-final variables. They can have both abstract as well as non-abstract methods. Let me give an example...

abstract class Shape 
{
    int color;
    // An abstract method
    abstract void draw();
}

A class can be abstract without having a single abstract method. But, it is important to declare a class abstract having at least one abstract method. To use it in Java, we need to extend it and provide a concrete class. An abstract class is commonly used to define a base class for a type hierarchy with default implementation, which applies to all child classes.

And yes the most important property of Abstract classes to be noticed is that they cannot be instantiated using new(), i.e. an object can't be created of an abstract class. And, this may have created doubt in you.

So, what do you think? Do these classes have constructors or not? Let's see...

Yes, these abstract classes can have a constructor in Java. As we know the compiler adds a default constructor with no argument in any class similarly in abstract classes too it adds or we can explicitly add a constructor to the abstract class.

Why can an abstract class have a constructor in Java?

Now if we can not create an instance of an abstract class then why does Java adds a constructor in the abstract class? One of the reasons which make sense is when any class extends an abstract class, the constructor of the sub-class will invoke the constructor of the super-class either implicitly or explicitly. This chaining of constructors is one of the reasons abstract classes can have constructors in Java.

Abstract data class is not used to create objects in Java and it is designed only to act as a base class to be inherited by other classes.

Here is an example Java program, which proves that abstract classes can have constructors in Java :

public class AbstractConstructorTest {

    public static void main(String args[]) {
       Vehicle bmw = new Car("BMW i8");
       server.startEngine();
    }
}

abstract class Vehicle{
    private final String name;

    public Vehicle(String name){
        this.name = name;
    }

    public abstract boolean startEngine();
}

class Car extends Vehicle{

    public Car(String name){
        super(name);
    }

    @Override
    public boolean startEngine() {
       System.out.println( this.name + " started successfully !!");
       return true;
    }

}

Output:
BMW i8 started successfully !!

In the above Java program, we have an abstract class Vehicle, which has a constructor with one parameter, which accepts the name. Subclass provides that name to the superclass while creating a concrete instance of Vehicle and overriding abstract method start(). From this, we can say abstract class can have a constructor. This can also be termed an example of constructor chaining.

Got that? No?

Okay, let me give you a brief knowledge about what exactly is this Constructor Chaining...

What is Constructor Chaining?

In Java, we can call one constructor from another and it’s known as constructor chaining in Java. Remember, constructor overloading and constructor chaining are two different things. Here, we are not discussing constructor overloading which I may share in my next coming blogs, so I am not going deep into it right now. Just remember, calling one constructor from another is called constructor chaining in Java.

And we are done!!🎉

I hope this first blog of mine has clarified your doubts & provided you with some useful information. Thanks for giving it time and making it to the end.

"It’s okay to be confused. Confusion is the route to all clarity in the world."

Find me on Twitter