Home Java javaTutorial Java interview essentials: FAQs on interfaces and abstract classes

Java interview essentials: FAQs on interfaces and abstract classes

Mar 04, 2024 am 09:07 AM

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();
}
}
Copy after login

Output:

Drawing a circle...
Drawing...
Show drawing...
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

See all articles