Home  >  Article  >  Java  >  Java interview essentials: FAQs on interfaces and abstract classes

Java interview essentials: FAQs on interfaces and abstract classes

WBOY
WBOYforward
2024-03-04 09:07:15405browse

Java 面试必备:接口与抽象类的常见问题解答

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!

  • interface:
    • Defines a set of method signatures, but no implementation details.
    • Declares the contract of the class, forcing the class that implements the interface to implement the specified method.
  • Abstract class:
    • Can contain both abstract methods (without implementation details) and concrete methods (with implementation details).
    • Declares the behavior of the class. Subclasses must implement abstract methods and can override specific methods.

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?

  • Using interface:
    • Define the functional contract of a class without specifying implementation details.
    • When polymorphism needs to be achieved (the same behavior is required, but the implementation is different).
  • Use abstract class:
    • Define a common set of behaviors and properties for a class, allowing descendant classes to extend.
    • When it is necessary to provide a default implementation and allow subclasses to specialize.

Q4: What is the relationship between multiple inheritance of interfaces and single-multiple inheritance of abstract classes?

  • Multiple inheritance of interfaces: A class can implement multiple interfaces, thereby inheriting the method signatures defined in these interfaces.
  • Single inheritance of abstract classes: A class can only inherit one abstract class, but polymorphism can be achieved by implementing multiple interfaces.

Q5: default and static methods in interfaces

Java 8 introduces the default and static methods of interfaces:

  • default method: An implemented interface method can be overridden by a class that implements the interface.
  • static method: Interface methods that are not implemented can be called without creating an instance of the interface.

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:

  • Final method: Cannot be overridden by subclasses.
  • static method: Associated with the class itself, not its instances.

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

  • Interface: cannot be instantiated directly, but can be instantiated through a class that implements it.
  • Abstract class: Can be instantiated, but cannot create objects. Its methods must be accessed indirectly by creating an instance of its subclass.

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

  • Mixed interfaces and abstract classes.
  • Wrong assumption that interfaces can contain concrete methods.
  • Forget that methods in interfaces are public and abstract by default.
  • It is believed that abstract classes can support multiple inheritance.
  • Cannot understand the role of default and static interface methods.

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!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete