Can abstract class have Constructor in Java - Interview Question

Yes, an abstract class can have a constructor in Java. You can either explicitly provide a constructor to abstract class or if you don't, the compiler will add default constructor of no argument in abstract class. This is true for all classes and it also applies to an abstract class. For those who want to recall what is an abstract class in Java, it's a class which can not be instantiated with new() operator or any other ways. In order to use an abstract class in Java,  You need to extend it and provide a concrete class. Abstract class is commonly used to define a base class for a type hierarchy with default implementation, which is applicable to all child classes. By the way, difference between interface and abstract class in Java is also one of the popular and tricky Java questions and should be prepared well for Java interviews.  


Can we declare constructor on abstract class in Java is follow-up of other  similar Java interview questions e.g. Can we override static method in Java?. Why does interviewer ask this questions? Mainly because, trying to confuse programmer with the fact that since an abstract class can not be instantiated, why abstract class needs constructor. In this Java Interview question article we will see that Abstract class can have a constructor in Java.



Why can an abstract class have a constructor in Java?

Now if we say we can not create an instance of an abstract class then why do 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 sub class will invoke the constructor of super class either implicitly or explicitly. This chaining of constructors is one of the reasons abstract class can have constructors in Java.



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

/**
 * Simple Java program to prove that abstract class can have constructor in Java.
 * @author http://java67.blogspot.com
 */
public class AbstractConstructorTest {

    public static void main(String args[]) {
       Server server = new Tomcat("Apache Tomcat");
       server.start();
    }
}

abstract class Server{
    protected final String name;
   
    public Server(String name){
        this.name = name;
    }
   
    public abstract boolean start();
}

class Tomcat extends Server{
   
    public Tomcat(String name){
        super(name);
    }

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

Output:
Apache Tomcat started successfully



In this example Java program, we have an abstract class Server, which has a constructor with one parameter, which accepts the name. Subclass provides that name to superclass while creating a concrete instance of Server and overriding abstract method starts(). Since this program compiles and run fine you can definitely say abstract class can have constructors in Java.

Can we declare constructor in abstract class in Java

Related Java Interview Questions from Java67 Blog

3 comments:

  1. since the abstract class is not initialized - there would not be any memory allocated since there is no object out of it right? if the variable in abstract class is private, then where will it be stored?

    ReplyDelete
    Replies
    1. private variable would be created in the memory when a concrete class gets created by using the abstract class, it is just you cannot access it without a getter method.

      Delete
  2. Hi I am David Mayer at https://www.java8certificationquestions.com/
    For more questions like this you can take a free test on our website.

    Thanks
    David

    ReplyDelete