10 Abstract Class and Interface Interview Questions Answers in Java (original) (raw)
Abstract class and interface are very popular in any object-oriented programming language or Java interview, and there are always one or more questions from this. The interface is more common, because of its popularity among designers but questions from abstract class also pop up now and then. Interview questions from the abstract class are more common on junior level or you say under 2 years experience of Java programmers while interface-related questions are mostly asked on senior-level Java interview e.g. 4 or 6 years of experience. They are mostly asked along with other Java design pattern questions, like the Decorator pattern or Factory pattern.
Anyway, in this article, we will see a mix of these interview questions from abstract class and interface. All questions have been asked in various Java interviews and the difficulty level for these questions is easy for most Java developers.
It’s mostly fact-based questions, but some questions like the difference between abstract class and interface in Java, and when to prefer abstract class over interface can be really tricky.
Btw, if you are new to the world of object-oriented programming and design then I also suggest you go through a hands-on course like UML and Object-Oriented Design Foundations by Karloy Neisztor on Udemy. It's a great course to understand the complete process of object-oriented analysis and design for creating quality software.
10 Frequently asked Abstract class and Interface questions in Java
Here is my list of questions, this not only explains rules related to abstract class but also shares some tricky questions about using abstract class and interface. If you have asked any question on this topic, which you don’t see in this list, then please share with us as a comment
1. Can abstract classes have constructors in Java?
Yes, an abstract class can declare and define a constructor in Java. Since you can not create an instance of an abstract class, a constructor can only be called during constructor chaining, i.e. when you create an instance of the concrete implementation class.
Now some interviewer, ask what is the purpose of a constructor if you can not instantiate abstract class? Well, it can still be used to initialize common variables, which are declared inside an abstract class, and used by the various implementation.
Also even if you don’t provide any constructor, the compiler will add a default no-argument constructor in an abstract class, without that your subclass will not compile, since the first statement in any constructor implicitly calls super(), default superclass constructor in Java.
2. Can abstract class implement interface in Java? do they require to implement all methods?
Yes, an abstract class can implement an interface by using the implements keyword. Since they are abstract, they don’t need to implement all methods. It’s good practice to provide an abstract base class, along with an interface to declare Type. One example of this is java.util.List interface and corresponding java.util.AbstractList abstract class.
Since AbstractList implements all common methods, concrete implementations like LinkedList and ArrayList are free from the burden of implementing all methods, had they implemented List interface directly.
It’s the best of both worlds, you can get the advantage of interface for declaring type, and flexibility of abstract class to implement common behavior in one place. Effective Java has a nice chapter on how to use interface and abstract class in Java, which is worth reading.
3. Can an abstract class be final in Java?
No, an abstract class can not be final in Java. Making them final will stop the abstract class from being extended, which is the only way to use an abstract class. They are also opposite of each other, abstract keyword enforces to extend a class, for using it, on the other hand, final keyword prevents a class from being extended.
In real-world also, abstract signifies incompleteness, while final is used to demonstrate completeness. The bottom line is, you can not make your class abstract and final in Java, at the same time, it’s a compile-time error.
4. Can abstract classes have static methods in Java?
Yes, an abstract class can declare and define static methods, nothing prevents doing that. But, you must follow guidelines for making a method static in Java, as it’s not welcomed in an object-oriented design, because static methods can not be overridden in Java. It’s very rare, you see static methods inside an abstract class, but as I said, if you have a very good reason for doing it, then nothing stops you.
5. Can you create an instance of an abstract class?
No, you can not create instances of abstract class in Java, they are incomplete. Even though, if your abstract class doesn’t contain any abstract method, you can not create an instance of it. By making a class abstract, you told the compiler that, it’s incomplete and should not be instantiated. Java compiler will throw an error when a code tries to instantiate an abstract class.
6. Is it necessary for an abstract class to have an abstract method?
No, It’s not mandatory for an abstract class to have any abstract method. You can make a class abstract in Java, by just using the abstract keyword in the class declaration. The compiler will enforce all structural restrictions, applied to an abstract class, like, not allowing to create of any instance.
By the way, it’s debatable whether you should have an abstract method inside the abstract class or interface. In my opinion, the abstract class should have abstract methods, because that’s the first thing programmer assumes when he sees that class. That would also go nicely along with the principle of least surprise.
7. Difference between abstract class and interface in Java?
This is the most important and one of the classic Java Interview questions. I don’t know, how many times I have seen this question at all most all levels of Java interviews. One reason, which makes this question interesting is the ability to produce examples.
It’s easy to answers questions on core OOP concepts like Abstraction, Encapsulation, Polymorphism, and Inheritance, but when it comes to subtle points like this, a candidate more often fumbled. You can see this post for all syntactical differences between abstract class and interface, but it deserves a post on its own.
8. When do you favor abstract class over interface?
This is the follow-up of previous interview questions on abstract class and interface. If you know the syntactical difference, you can answer this question quite easily, as they are the one, which drives the decision. Since it’s almost impossible to add a new method on a published interface, it’s better to use abstract class, when evolution is a concern.
Abstract class in Java evolves better than the interface. Similarly, if you have too many methods inside the interface, you are creating pain for all its implementation, consider providing an abstract class for default implementation. This is the pattern followed in the Java collection package, you can see AbstractList provides a default implementation for the List interface.
9. What is the abstract method in Java?
An abstract method is a method without a body. You just declare the method, without defining it and use abstract keywords in the method declaration. All methods declared inside Java Interface is by default abstract. Here is an example of an abstract method in Java
public void abstract printVersion();
Now, In order to implement this method, you need to extend the abstract class and override this method.
10. Can an abstract class contains the main method in Java?
Yes, an abstract class can contain the main method, it just another static method and you can execute the Abstract class with the main method until you don’t create any instance.
That’s all on this list of interview questions on abstract class, interface, and methods in Java. You can also bookmark this list as the FAQ for abstract class, nice to refresh, before going to the interview. On a different note, abstract class and interface are key design decisions on object-oriented analysis and design process and should be taken by applying due diligence, of course, if you want to write flexible systems.
Further Learning
SOLID Principles of Object-Oriented Design
Absolute Introduction to Object-Oriented Programming in Java
Java - Object-Oriented Programming [For Absolute Beginners]