Monday, February 11, 2013

Can abstract class have Constructor in Java - Interview Question

Yes, abstract class can have constructor in Java. You can either explicitly provide constructor to abstract class or if you don't, compiler will add default constructor of no argument in abstract class. This is true for all classes and its also applies on abstract class. For those who want to recall what is an abstract class in Java, its a class which can not be instantiated with new() operator or any other ways. In order to use abstract class in Java,  You need to extend it and provide a concrete class. Abstract class is commonly used to define 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 followup of other  similar Java interview questions e.g. Can we override static method in Java?. Why interviewer ask this questions? Mainly because, trying to confuse programmer with fact that since abstract class can not be instantiated, why abstract class need constructor. In this Java Interview question article we will see that Abstract class can have constructor in Java.

Why abstract class have constructor in Java

Can we declare constructor in abstract class in JavaNow if we say we can not create instance of abstract class then why do Java adds constructor in abstract class. One of the reason which make sense is,  when any class extend abstract class, constructor of sub class will invoke constructor of super class either implicitly or explicitly. This chaining of constructors is one of the reason 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 one parametric constructor, which accepts name. Subclass provides that name to superclass while creating concrete instance of Server and overriding abstract method start(). Since this program compile and run fine you can definitely say abstract class can have constructors in Java.

Related Java Interview Questions from Java67 Blog

1 comment:

  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

Java67 Headline Animator