php editor Apple has compiled a necessary guide for Java interviews: Frequently Asked Questions about Interfaces and Abstract Classes. In Java interviews, interfaces and abstract classes are important topics that are often asked. Through this article, you will learn about the differences, usage scenarios, advantages and disadvantages of interfaces and abstract classes, etc., which will help you better prepare for interviews and demonstrate your technical capabilities. Let’s dive into these key questions to support your Java interview!
Q2: What is the difference between interface and abstract class?
feature | interface | Abstract class |
---|---|---|
method | can only be declared, not implemented | can be declared and implemented |
accomplish | Must be implemented by the class that implements it | Can be achieved through subclasses |
Multiple inheritance | Support multiple inheritance | Multiple inheritance is not supported |
Instantiation | Cannot be instantiated | Can be instantiated (but cannot create objects) |
Q3: When to use interface or abstract class?
Q4: What is the relationship between multiple inheritance of interfaces and single-multiple inheritance of abstract classes?
Q5: default and static methods in interfaces
Java 8 introduces the default and static methods of interfaces:
Demo code:
interface Drawable { void draw(); default void print() { System.out.println("Drawing..."); } static void show() { System.out.println("Show drawing..."); } } class Circle implements Drawable { @Override public void draw() { System.out.println("Drawing a circle..."); } } public class Main { public static void main(String[] args) { Drawable circle = new Circle(); circle.draw(); Drawable.print(); Drawable.show(); } }
Output:
Drawing a circle... Drawing... Show drawing...
Q6: Final and static methods in abstract classes
Abstract classes can contain final and static methods:
Q7: Access permissions in interfaces and abstract classes
All methods and constants in the interface are public and abstract by default. Methods in abstract classes can have various access rights, but abstract methods are public and abstract by default.
Q8: Instantiation and calling of interfaces and abstract classes
Q9: Comparison of interfaces and abstract classes
feature | interface | Abstract class |
---|---|---|
Polymorphism | support | support |
Abstract method | Must declare | can be declared |
specific method | not support | can be declared |
Multiple inheritance | support | not support |
Instantiation | Cannot be instantiated | Can be instantiated, but cannot create objects |
debug | Difficulty (doing it in the implementation class) | Relatively easy (do it in an abstract class) |
Flexibility and Scalability | More flexible and more scalable | Low flexibility and weak scalability |
Q10: Common trap questions in interviews
The above is the detailed content of Java interview essentials: FAQs on interfaces and abstract classes. For more information, please follow other related articles on the PHP Chinese website!