Monday, April 10, 2023

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 the abstract class or if you don't, the compiler will add a default constructor of no argument in the 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 that can not be instantiated with the new() operator or any other way. In order to use an abstract class in Java,  You 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 is applicable to all child classes. 

By the way, the 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 a follow-up of other similar Java interview questions e.g. Can we override static method in Java?. Why does the interviewer ask these questions? Mainly because, trying to confuse programmer with the fact that since an abstract class can not be instantiated, why abstract class needs a 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 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 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.




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 runs 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

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

4 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. Why wouldn't an abstract class be initialized?

      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

Feel free to comment, ask questions if you have any doubt.