Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Saturday, July 1, 2023

What is Abstract Class and Method in Java? Examples and Frequently asked Questions

Both abstract class and abstract methods are very important concepts in core Java and every Java developer should have good understanding of what they are, when to use them and how to use them. They are also very popular on core Java job interviews as well as on Oracle Java certifications e.g. OCAJP or OCPJP.  You will often find a lot of code based questions testing essential core Java concepts based upon abstract class and methods in the real exam. All good mock exam simulators e.g. Whizlabs and Certification-questions also contains a lot of questions testing and explaining these key concepts, which shows how important they are for Java developers. 

In the past, I have shared 10 abstract class and interface interview questions where I have touched abstract class briefly but In this article, I have collated some of the frequently asked questions about abstract class and abstract methods in Java which goes more in-depth and covers many aspect of abstract class and methods like creating instances, calling them, inheritance and implementation and so on. 

You can review these questions before going for Java interview, both telephonic and face-to-face round. They are also very useful if you are preparing for Oracle Java certification e.g. OCAJP.

You can answer almost all of these questions if you have a couple of years of experience in core Java but if you struggle to answer most of them, maybe it's time to revise those concepts by reading a good core Java book e.g. The Java Complete reference. 

20 Abstract class and Method Interview Questions for 2 to 5 Years Experienced

So, without any further ado, here is my a list of 20 abstract class and method related interview questions from various Java interviews which will help you to understand abstract class and method in Java in great depth. These questions are not just important from interview point of view but also to learn one of the important core Java feature, I mean abstraction, in particular abstract class and methods in Java. 


1. What is an abstract class in Java? (detailed answer)
An abstract class in Java is a special class which cannot be instantiate. In other words, it means an incomplete class in Java. You make a class abstract to ensure that anyone who wants to use it must provide a concrete implementation of it. There are several examples of abstract classes in java e.g. AbstractList or AbstractCollection.

Prior to Java 8 they are used to provide skeleton implementation of corresponding interface e.g. AbstractList provides an implementation of List interface while AbstractCollection provides implementation of Collection interface. 

Now, it become easier for any class to extend AbstractList instead of implementing List interface. It just need to override the method it want instead of implementing every methods defined in List interface. From Java 8 onwards, you don't need this because you can also provide default implementation using default methods in the interface itself. 

What is Abstract Class and Method in Java? Examples and Frequently asked Questions




2. What is an abstract method in Java? (detailed answer)
An abstract method is a special method in Java which doesn't have a method body. It just declare a method without defining it. An abstract method can exists in interface or abstract class and they were added to define an API. The concrete implementation of abstract method is provided by overriding it in subclass. An example of abstract method is run() method of java.lang.Thread class. 


3. What is difference between a concrete class and an abstract class in Java? (detailed answer)
The key difference between an abstract class and concrete class is that you can create instance of concrete class but you cannot instantiate an abstract class in Java. The Java compiler doesn't allow that because an abstract class is considered incomplete. It may or may not contain abstract method but you must extend it to provide concrete implementation which can be instantiated.


4. Can an abstract class have constructor in Java? (detailed answer)
Yes, you can declare and define a constructor inside an abstract class in Java, but you cannot instantiate an abstract class, that would result in compile time error. The constructor is used to initialize common fields defined in the abstract class and it is invoked when a client create instance of a concrete implementation of abstract class in Java. 


5. Can you make an abstract class final in Java? (detailed answer)
No, you cannot make an abstract class final in Java because both abstract and final keyword are mutually exclusive. This means you cannot use them together in a class declaration. It is also a common sense because abstract class represent an incomplete class which can only be completed by extending it and a final class represent a concrete class which should not be extended. 

By making an abstract class final, you prohibit it from extension, which means it can never be complete. Hence, Java specification doesn't allow you to make an abstract class final in Java. If you do so, then it will result in compile time error. 


63. Can you make an abstract method final in Java? (detailed answer)
Now, you can't make an abstract method final in Java due to the same reasons you can't make abstract class final. Similar to abstract class, an abstract method is also incomplete and you must override to complete it. Since, you cannot override a final method in Java, it make no sense to make an abstract method final in Java. Again, it's a compile time error to do so. 


7. Can you instantiate an abstract class in Java? (detailed answer)
This is the follow-up of the first question, when candidate says that an abstract class can have constructors defined, Interviewer ask if it is possible to instantiate an abstract class in Java? Answer is No, it's not possible even that abstract class can have constructors. It's a compile time error trying to create object of abstract class in Java. 


8. Can an abstract class have static methods in Java? (detailed answer)
Yes, an abstract can have static methods in Java, there is no problem with that. Here is a code example to prove this point:

public abstract class Consumer {

    public abstract void consume();

    public static void main(String args[]) {
        System.out.println("Inside abstract class");
    }
}

Sine main method is a static method in Java, it proves the point that abstract class can have static method in Java. 

9. Can an abstract class have final method in Java? (detailed answer)
Yes, an abstract class may contain final method, there is nothing wrong with that. Here is a code example to prove that point
static abstract class Poppy {
    
    public abstract void dance();
    
}

When you compile this code, there is no compile time error which proves that you can have a final method in abstract class in Java.


10. Can a final class have an abstract method in Java? (detailed answer)
This is one of the tricky question in Java. It is also asked as follow-up of previous question, candidate think if an abstract class contain final method then why not final class contain abstract method, but they fail to recognize that once make a method abstract in Java, the enclosing class automatically become an abstract class. 

Since you cannot make an abstract class final in Java, it becomes illegal. Hence, it's not possible to have an abstract method in final class. It is also a bit of common sense, a final class cannot be extended and to make an abstract method useful, you must override it, but you cannot override a method without extending that class.  

This is also one of the popular questions on Java interviews on final modifier and can be really tricky if you don't know both abstract and final concept well. 


11. Can you make an abstract class static in Java? (detailed answer)
The answer is both, Yes and No. You cannot make a top level abstract class static in Java as discussed in previous article, but you can always make a nested class static in Java as shown in follow example:
// compile time error
public abstract class TopLevel {

}

// OK
public class Container {
    public abstract class Component {
        public abstract void paint();
    }
}

You can compile this program using javac to verify that fist will give you compile time error but second will just compile fine. 


12. Can an abstract class implements interface in Java? (detailed answer)
Yes, an abstract class can implement an interface in Java. There is no problem with that. It can implement abstract methods inherited from interface or leave them un-implemented because its an abstract class, both are fine. 


13. What is the use of constructor in abstract class in Java? (detailed answer)
This is another interesting and tricky core Java question, usually asked as follow-up of question no 1 or 7 i.e. whether abstract class can have constructor or not and can you instantiate abstract class in Java or not? 

The real use of constructor in abstract class is to initialize the state variables maintained in the abstract class. The constructor is usually executed when you create instance of a concrete class which implement this abstract class. 


14. Can an abstract class have final variables in Java? (detailed answer)
Yes, an abstract class have final variables in Java. This question is usually asked to confuse candidates because an abstract class cannot be final in Java, but it can contain both final variables as well as final methods as shown in following example:
static abstract class Compiler {
    public final int version = 1;

    public final int getVersion() {
        return version;
    }
    public abstract void compile();
}

You can see that above code contains both final variables and final methods inside abstract class in Java. 


15. Can an abstract class have static variables in Java? (detailed answer)
Yes, an abstract class may contain static variable in Java, there is no problem with that. IT can even contain static methods as shown in following example:
static abstract class JVM {
    public static final int version = 1;

    public static final int getVersion() {
        return version;
    }
    public abstract void execute();
}
It generally a good practice to make static variable final in Java to prevent someone changing its value. It's much safer approach. 


16. Can you make a class abstract without any abstract method? (detailed answer)
Yes, Java specification allows you to mark a class abstract even if it doesn't contain an abstract method as shown in following example :
static abstract class JVM {
    public void info() {
        System.out.println("I am a JVM");
    }
}

Like any other abstract class, you cannot instantiate this class unless you create a concrete class by extending it.


17. What is difference between a normal method and abstract method in Java? (detailed answer)
The key difference is that an abstract method doesn't have body, it only have declarations like:

public abstract int count();

while a concrete method always have method body as shown below:

public int count(){
  return count;
}

Also, when an abstract method can only exists in an abstract class but a concrete method can exists in both normal class as well as on abstract class. 


18. Is it possible to inherit from multiple abstract classes in Java? (detailed answer)
No, as per Java specification multiple inheritance is not allowed, which means a class can only extend one class, hence its not possible to inherit from multiple abstract class in Java. A class can only extend at most one abstract class in Java. 


19. Why abstract class cannot be static in Java? (detailed answer)
The question is wrong, an abstract class can be static. Only a top level abstract class cannot be static because it doesn't make sense as discussed here, but you can make a nested abstract class static in Java as shown below:

public class Outer {

    static abstract class Inner {

    }
}

The code will compile fine which proves that a nested abstract class can be static in Java. 


20. What is difference between an abstract class and an interface in Java? (detailed answer)
This is one of the most frequently asked question in this topic. I have seen abstract class vs interface question so many times in my career and thankfully it gives you good opportunity to showcase your OOP skills. 

The key difference is that interface can only contain public methods and that's why they are used to specify API but abstract class may contain private methods or implementations in Java. A class can also implement multiple interface but only one abstract class in Java. You can read a more detailed answer of this question here and here. 


That's all about what is abstract class and method in Java with example as well a couple of the frequently asked questions about abstract class and methods in Java. These are very important concepts, both from Java interview point of view and Java certification point of view e.g. OCAJP or OCPJP. 

If you don't know answers of these questions, maybe it's a good time to revise these concepts by reading a good book on core Java e.g. Java: Complete Reference, Ninth Edition or checking out official Java documentation.

Other Interview Questions articles you may like
  • 40+ Object-Oriented Interview Questions with Answers (questions)
  • 50 Java 8 Interview Questions with answers (java 8 questions)
  • 15 Spring Data JPA Interview Questions (list)
  • 10 Dynamic Programming PRoblems for Coding interviews (questions)
  • 25 Spring Security Interview Questions with Answers (questions)
  • 5 Best Courses to learn Java Programming  (best courses)
  • 130+ Java Interview Questions with Answers (list)
  • 17 Spring AOP Interview Questions with Answers (list)
  • 20+ JUnit Interview Questions for Java developers (questions)
  • 35 Python Interview Questions for Beginners (python questions)
  • 50+ SQL and Database Phone Interview questions (SQL questions)
  • 20 Spring MVC Interview Questions with answers (spring questions)



No comments :

Post a Comment