JavaRush /Java Blog /Random EN /10 Abstract Classes and Interfaces Java Interview Questio...

10 Abstract Classes and Interfaces Java Interview Questions

Published in the Random EN group
Abstract classes and interfaces are very popular in all object-oriented programming languages. And in almost every Java interview you come across at least one question on this topic. Interfaces are mentioned more often due to their popularity among software designers, but questions about abstract classes also come up from time to time. The latter are most often asked of applicants for the position of junior developers, say, with no more than two years of experience in Java development, while questions about interfaces are most often encountered during interviews with those whose experience has already exceeded four years. They are generally asked in conjunction with other questions about Java design patterns, such as the Decorator or Factory patterns. 10 Abstract Classes and Interfaces Java Interview Questions - 1In this article, we will look at common questions on abstract classes and interfaces that were asked at various levels of Java interviews. Most of them should not be difficult even for a novice Java programmer. These are mostly pure knowledge questions, but some of them, such as the differences between abstract classes and interfaces in Java, or when to choose an abstract class over an interface , can be quite tricky. We offer you a dozen interesting questions on the topic.
If you've ever been asked a question in an interview or had to ask any worthwhile question about abstract classes and interfaces, but it's not on this list, please share it in the comments.

1. Can an abstract class have constructors in Java?

Yes, in an abstract class in Java you can declare and define constructors. Since it is impossible to create instances of abstract classes, such a constructor can be called only when forming a chain of constructors, that is, when creating an instance of a specific implementation class. But imagine that the interviewer then asks the question: what’s the point of a constructor if you can’t create an instance of an abstract class anyway? The point is that it can still be used to set the initial values ​​of common variables declared in an abstract class and used by various implementations. Even if you don't declare any constructor, the compiler will add a default no-argument constructor to the abstract class. Without it, your subclass will not compile because the first statement in any constructor is an implicit call super()to the default superclass constructor in Java.

2. Can abstract classes in Java implement interfaces? Do they have to implement all methods?

Yes, abstract classes can implement interfaces using the implements. Since they are abstract, they are not required to implement all methods. Having an abstract base class and an interface to declare the type is a recommended practice. An example is an interface java.util.Listand the corresponding abstract class java.util.AbstractList. Because AbstractListit implements all common methods, specific implementations (such as LinkedListand ArrayList) do not have to implement all methods, as would be the case if they implemented the interface Listdirectly. This solution combines the benefit of using an interface to declare a type with the flexibility of an abstract class to implement all the common behavior in one place. In Joshua Bloch's book "Java. Effective Programming” has an excellent chapter on the topic of using interfaces and abstract classes in Java, for a better understanding it makes sense to study it.

3. Can an abstract class be final?

No, he can not. The keyword finalmeans that the class is at the top of the hierarchy and cannot have descendants. And an abstract class without heirs is a spherical horse in a vacuum, since it is impossible to create an instance abstract class. Thus, if a class is both abstractand final, then it has no descendants and cannot be instantiated. The Java compiler will throw an error if you make a class abstractand final.

4. Can an abstract class in Java have static methods?

Yes, abstract classes can declare and define static methods. It is only necessary to follow the general principles of creating static methods in Java, since they are undesirable in object-oriented design, because overriding static methods in Java is not possible. Static methods in an abstract class are very rare, but if there are good reasons for it, there is nothing stopping you from using them.

5. Is it possible to instantiate an abstract class?

No, you can't do this. The essence of an abstract class is that it is not complete, and it needs to be completed in its descendant classes. That is, this class is not ready for use. For example, it may lack the implementation of some methods. Since a class is not ready for use, its object cannot be created. But you can create instances of inheritors of an abstract class. The Java compiler will throw an error if a program tries to instantiate an abstract class.

6. Is it necessary for an abstract class to have abstract methods?

No, an abstract class may not have any abstract methods. You can make a class abstract in Java simply by using a keyword abstractin its declaration. The compiler will enforce any structural restrictions, such as not allowing instantiations of this class to be created. By the way, the question of whether there should be abstract methods in an abstract class or interface is controversial. It seems to me that an abstract class should have abstract methods, since this is the first thing a programmer thinks of when seeing an abstract class. This fits well with the principle of minimizing surprises.

7. What are the differences between an abstract class and an interface in Java?

This is the most important and one of the most classic Java interview questions. I can't count how many times I've seen this question in Java interviews at all levels. What makes this question interesting is, in particular, the opportunity for the applicant to provide an example. Answering questions about the basics of object-oriented programming, such as abstraction, encapsulation, polymorphism and inheritance, is easy, but when it comes to such subtle nuances, job applicants often get confused and say the first thing that comes to mind. The answer to this question deserves a separate article (especially after changes in Java 8), however, in short:
  • An interface describes only the behavior (methods) of an object, but it does not have states (fields) (except public static final), while an abstract class can have them.

  • An abstract class is inherited (extends), and an interface is implemented (implements). We can inherit only one class, but implement as many interfaces as we like. An interface can extend (extend) another interface/interfaces.

  • Abstract classes are used when there is an "is-a" relationship, that is, the subclass extends the base abstract class, and interfaces can be implemented by different classes that are not at all related to each other.

8. When does it make sense to prefer an abstract class to an interface and vice versa?

This is a continuation of previous questions on abstract classes and interfaces. If you know what their syntactic differences are, then answering this question will not cause you problems, since they are the determining factor in making a decision. Since it is almost impossible to add a new method to a published interface, it is better to use an abstract class in case of potential need for further development. Developing abstract classes in Java is easier than developing interfaces. Likewise, if an interface has too many methods and implementing them all becomes a real headache, it is better to create an abstract class for the default implementation. This pattern is followed in the Java collections package, the abstract class AbstractListprovides the default implementation for the List. Use abstract classes if:
  • You want to share code between several closely related classes.

  • You expect classes that extend your abstract class to have many methods or fields in common, or to require access modifiers other than public(for example, protectedand private).

  • You want to declare non-static or не-finalfields. This allows you to define methods that can access and change the state of the object to which they belong.
Use interfaces if:
  • You expect unrelated classes to implement your interface. For example, interfaces Comparableand Cloneableare implemented by many unrelated classes.

  • You want to define the behavior of a particular data type, but you don't care who implements it.

  • You want to use multiple type inheritance.

9. What is an abstract method in Java?

An abstract method is a method without a body. You simply declare a method without defining it, using a keyword abstractin the method declaration. All methods declared inside an interface in the Java language are abstract by default. Here is an example of an abstract method in Java:
public void abstract printVersion();
Now, to implement this method, you need to extend the abstract class and override this method.

10. Can an abstract class in Java contain a method main?

Yes, an abstract class in Java can contain a method main, because it is just another static method, and an abstract class can be executed using the method main, as long as you do not instantiate it. That's all I wanted to tell you. And remember: abstract classes and interfaces are key design decisions in the object-oriented analysis and design process, and should be used with due caution, of course, if you want to create a flexible system.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION