Friday, June 19, 2015

Top 10 Java Abstract Class Interview Questions

Abstract class questions are generally asked for junior level interviews. 

And for higher level they are coupled with interfaces. Interfaces though considered an inferior sibling of interface, has a lot of aces up its sleeve when it comes to interviews.

Let's have a look at what questions can crop up in interviews. See this post for Interface Interview Questions.

1. Explain Abstract Class.

Abstract class contains either all abstract functions or a combination of abstract/concrete functions, or all abstract functions. A class extending the abstract class, need not to implement all the functions of the abstract class. An abstract class can have any type of variables.

2. What is the difference between abstract class and interface?

Be sure to get this question for junior level interviews.

Abstract Class
Contains either abstract method or concrete methods
Contains only method declaration. No concrete methods allowed.
Class which extends abstract class, need not define all methods
Compulsory for implementing to override all methods
Used when we want to define general features at one place with each extending class defining specialization
Used when we want to define all behavior type at a single place, with implementing unique way of same behavior type
Variables can be defined as suitable
Variable are public static final by default
Fast processing as tightly coupled
Loosely coupled. Hence processing is a bit slow.

3. Can abstract class be instantiated?

No an abstract class can't be instantiated. We can only instantiate a subclass of the abstract class. Condition is that sub class should also not be abstract.

4. What is 'Abstract Interface'?

All interfaces are abstract interfaces. All methods are abstract (method signatures) in an interface.

5. Can abstract class have constructors?

Unlike Interfaces, Abstract class can have a constructor. To access the constructor, we have to first extend the abstract class. Then the subclass can be instantiated and the constructor will be called (if not overridden). 

6. Abstract Class implementing an Interface. What would be the behaviour?

In such a case, abstract class need not implement all methods of the interface as it is abstract.

7. What happens if we define abstract class as final?

It's not permitted in Java. A final class can't be extended. That defeats the very purpose of abstract class. Abstract and Final are in fact opposites of each other.

8. Explain abstract method in Java.

A method without a body and with abstract keyword is an abstract method. It doesn't have a body just a declaration. Abstract class need to be extended and the method implemented.

9. What methods can be there inside abstract class?

Abstract class can have both abstract and concrete methods. Not compulsory to have all methods abstract.

10. Can we have static method inside an abstract class?

Theoretically yes, but in practice we should avoid it. As static method can't be overridden in a subclass and also it is to be used directly with ClassName without instantiation. This defeats the purpose of the abstract class.

1 comment: