What is Abstract class in Java and popular interview questions?

UPDATED: 15 November 2019
What is Abstract Class?
An abstract class is a class that is incomplete, or to be considered incomplete.

You can declare abstract methods for which non abstract subclass has to provide implementation or it'll give compile-time error. "Methods that are declared but not yet implemented."

Also you can provide method implementation in abstract class itself. It can be used from subclass. You can override these method in its subclass.

Source code (Mobile.java)
import java.util.Date;
public abstract class Mobile {

    /**
     * Sub class has to provide implementation for method `call()`
     */ 
    public abstract void call();

    /**
     * Sub class has to provide implementation for method `sms()`
     */ 
    public abstract void sms();

    /**
     * We've provided implementation for current time.
     */
    public Date currentTime(){
     return new Date();
    }
}
Source code (SmartMobile.java)
public class SmartMobile extends Mobile{
 
    public void call(){
     // provide implementation
    }

    public void sms(){
     // provide implementation
    }
}
Source code (Main.java)
public class Main(){
    public static void main(String[] args){
     /** You can not instantiate abstract class. 
      * Following line will cause Compile-Time error 
      */
     // Mobile mobile = new Mobile();

     Mobile smartMobile = new SmartMobile();
    }
}

Now lets discuss behavior of abstract class via question-answer. These are the popular abstract class interview questions .

Question: Can we instantiate abstract class?
Answer: No. We can not instantiate abstract class. Its restricted by Java.

Question: Why does abstract class have constructor if we can not instantiate? 
Answer: Constructor in abstract class used to initialize properties/fields of abstract class via not abstract sub-class.

Question: When constructor of abstract class called?
Answer: When sub-class instantiated, constructor of abstract class is called.

Question: Can we mark constructor as abstract?
Answer: No

Question: Is it compulsory for abstract class to have at least one abstract method?
Answer: No its not compulsory. You can create abstract class with or without abstract methods.

Question: Can we declare abstract method as private? 
Answer: No. If abstract method is private then its not visible/accessible in its sub-class so it can not provide its implementation. However you can make abstract method protected.

Question: Why final and abstract can not be used at a time? 
Answer: When class or method marked with final means value or implementation provided and you don't want it to be changed while abstract means implementation asked to provide.

Simply it contradicts with each other.

Question: Can we declare abstract class or method as static?
Answer: No. static can be called without creating object and it contains implementation or value. abstract means implementation asked to provide. Its same as final.

Simply it contradicts with each other.

Question: Can we declare inner class as abstract?
Answer: Yes

Question: Can abstract method include throws in its declaration?
Answer: Yes

Question: Can we mark abstract method as synchronized?
Answer: No. However sub-class which override the method can be synchronized.
public abstract class Demo{
    public abstract void test();
}

public class DemoImpl extends Demo{
    @Override
    public synchronized void test(){
        //implementation
    }
}

Question: Can we use abstract class as member of another abstract class?
Answer: Yes. Its same as declaring other class as member(variable).

Reference
Difference between Abstract class and Interface in Java

0 comments :