JavaRush/Java Blog/Random EN/10 questions on abstract classes and interfaces from Java...

10 questions on abstract classes and interfaces from Java interviews

Published in the Random EN group
members
Abstract classes and interfaces are very popular in all object-oriented programming languages. And almost every Java interview comes across at least one question on this topic. Interfaces are mentioned more often because of their popularity among software designers, but there are also questions about abstract classes from time to time. The latter are more often asked by applicants for junior developers with, say, no more than two years of Java development experience, while interface questions are most often asked when interviewing those whose experience has already exceeded four years. Basically, they are asked along with other questions about Java design patterns, such as the Decorator or Factory patterns. 10 questions on abstract classes and interfaces from Java interviews - 1In this article, we will look at frequently asked questions about abstract classes and interfaces that were asked at Java interviews at various levels. Most of them should be easy even for a beginner Java programmer. These are mostly open-ended questions, but some of them, such as the difference between abstract classes and interfaces in Java, or when to prefer an abstract class over an interface , can be tricky. We offer you a dozen interesting questions on the topic.
If you have ever been asked in an interview or had to ask any noteworthy question about abstract classes and interfaces, but it is not on this list, share it in the comments.

1. Can an abstract class have constructors in Java?

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

2. Can abstract classes in the Java language implement interfaces? Should they 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 for the type declaration is a recommended practice. An example is an interface java.util.Listand the corresponding abstract class java.util.AbstractList. Because AbstractListit implements all generic methods, concrete implementations (for example, LinkedListand ArrayList) do not need to implement all methods, as they would if they implemented the interface Listdirectly. This solution combines the advantage of using an interface to declare a type with the flexibility of an abstract class to implement all common behavior in one place. In Joshua Bloch's Java. Effective Programming»There is an excellent chapter on the use of 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 children. And an abstract class without descendants is a spherical horse in a vacuum, since you cannot instantiate abstract class. Thus, if a class is both abstractand final, then it has no heirs and its instance cannot be created. The Java compiler will throw an error if you make a class abstractand final.

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

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

5. Is it possible to instantiate an abstract class?

No, this cannot be done. The essence of an abstract class is that it is not completed, and it must be completed in the descendant classes. That is, this class is not ready for use. In it, for example, there may be no implementation of some methods. Since the class is not ready for use, you cannot create its object. But instances of the heirs of an abstract class can be created. The Java compiler will throw an error if the program tries to instantiate an abstract class.

6. Does an abstract class have to have abstract methods?

No, an abstract class may not have any abstract methods. Making a class abstract in the Java language is as simple as using a keyword abstractin the declaration. The compiler will enforce any structural restrictions, such as not allowing instances 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 debatable. It seems to me that an abstract class should have abstract methods, since this is the first thing a programmer thinks of when he sees an abstract class. This is in good agreement with the principle of minimizing surprises.

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

This is one of the most important and one of the most classic Java language interview questions. I can't count how many times I've come across this question in Java interviews for 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 talking about abstraction, encapsulation, polymorphism, and inheritance, is easy, but when it comes to such subtle nuances, job applicants very often get lost and say the first thing that comes to mind. The answer to this question pulls on a separate article (especially after the changes in Java 8), however, in short:
  • An interface describes only the behavior (methods) of an object, but it has no states (fields) (except for 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, and implement interfaces as many as we like. An interface may extend other interface(s).

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

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. Because it's almost impossible to add a new method to a published interface, it's better to use an abstract class if you need to modify it. It is easier to develop abstract classes in Java than interfaces. Similarly, if there are too many methods in an interface and it becomes a real pain to implement them all, it is better to create an abstract class for the default implementation. This pattern is also followed in the Java Collections package, an abstract class AbstractListproviding a 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 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 they belong to.
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 the Java language?

An abstract method is a method without a body. You simply declare a method without defining it, using the 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 the Java language:
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's just another static method, and an abstract class can be done with a method mainif you don't instantiate it. That's all I wanted to tell. And remember, abstract classes and interfaces are key design decisions in the process of object-oriented analysis and design, and should be used with due diligence, of course, if you want to create a flexible system.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet