迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中接口和抽象类的区别

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍了 Java 中接口和抽象类之间的区别,并列出了一些示例代码来理解该主题。

抽象是隐藏实现并且只向用户提供基本细节的过程。Java 中的抽象是通过抽象类和接口实现的。抽象类和接口有一些共同点,但它们之间有很大的不同。让我们先来看看两者之间的一些相似之处。


抽象类和接口之间的相似之处

  • 如上所述,抽象类和接口都用于抽象。
  • 抽象类和接口无法实例化,即我们无法为它们创建对象。
  • 子类必须覆盖抽象类或接口中定义的抽象方法。

以上几点几乎总结了两者之间的相似之处。现在让我们看看它们之间的一些主要区别。


抽象类和接口的区别

抽象类 界面
Java 中的 abstract 关键字用于创建或声明抽象类。 在 Java 中,interface 关键字用于创建或声明新接口。
类可以通过使用 extends 关键字来继承抽象类的属性和方法。 要在 Java 中实现接口,我们可以使用 implements 关键字。
抽象类中可以定义抽象或非抽象方法。抽象方法是没有为它们提供任何实现的方法。 一个接口中只能有抽象方法。我们只能提供方法定义而不能提供其实现。在 Java 8 之后,我们还可以在接口中使用默认方法和静态方法。
抽象类可以在其中包含最终或非最终变量(类属性)。它还可以包含静态或非静态属性。 一个接口只能包含静态成员和最终成员,不允许有其他类型的成员。
抽象类可以实现接口并实现接口的方法。 接口不能扩展任何其他类,也不能覆盖或实现抽象类方法。
抽象类可以扩展其他类,也可以实现接口。 正如前一点所讨论的,接口不能扩展其他类。但是实现接口没有限制。
Java 不支持通过类进行多重继承。抽象类,就像任何其他类一样,不支持多重继承。 Java 中的多重继承支持是通过接口提供的。这是因为接口提供了完整的抽象。
抽象类成员或属性可以是私有的、受保护的或公共的。 接口的属性或成员始终是公共的。

什么时候使用抽象类和接口

抽象类可以提供部分或完全抽象。另一方面,接口总是提供完整的抽象。可以为一些具有一些共同功能的类创建一个抽象父类。如果你想要更多的行动自由,抽象类也是首选。

当我们想要定义一个基本结构时,接口是首选。然后程序员可以用这个结构构建任何东西。接口还支持多重继承。所以单个类可以实现多个接口。

总的来说,这是一个选择问题和需要完成的任务。抽象类和接口都适用于不同的目的,应该相应地使用。


Java 中的抽象类

让我们创建一个抽象类并创建扩展它的子类以了解抽象类及其功能。

abstract class Bell {
  protected String sound;

  Bell() {
    this.sound = "ting";
  }
  // Abstract Method
  abstract public void ring();

  // Non-Abstract Methods
  public void increaseVolume() {
    System.out.println("Increasing Volume");
  }

  public void decreaseVolume() {
    System.out.println("Decreasing Volume");
  }
}
class SchoolBell extends Bell {
  @Override
  public void ring() {
    System.out.println("Ringing the School bell: " + sound);
  }
}
class ChruchBell extends Bell {
  @Override
  public void ring() {
    System.out.println("Ringing the Chruch Bell: " + sound);
  }
}
public class AbstractClassDemo {
  public static void main(String[] args) {
    SchoolBell sb = new SchoolBell();
    ChruchBell cb = new ChruchBell();

    // Using the overridden methods
    sb.ring();
    cb.ring();

    // Using the non-abstract methods of Bell class
    sb.increaseVolume();
    cb.decreaseVolume();
  }
}

输出:

Ringing the School bell: ting
Ringing the Chruch Bell: ting
Increasing Volume
Decreasing Volume

Java 中的接口

让我们使用接口复制相同的场景。我们不能再在接口中定义非抽象方法。如果类不想要 increaseVolume()decreaseVolume() 方法的公共实现,接口是正确的选择。

interface Bell {
  String sound = "ting";
  // only abstract methods allowed in interface
  public void ring();
  public void increaseVolume();
  public void decreaseVolume();
}
class SchoolBell implements Bell {
  public void ring() {
    System.out.println("Ringing the School bell: " + sound);
  }
  @Override
  public void increaseVolume() {
    System.out.println("Increasing Volume of School Bell");
  }
  @Override
  public void decreaseVolume() {
    System.out.println("Decreasing Volume of School Bell");
  }
}
class ChruchBell implements Bell {
  public void ring() {
    System.out.println("Ringing the Chruch Bell: " + sound);
  }
  @Override
  public void increaseVolume() {
    System.out.println("Increasing Volume of Chruch Bell");
  }
  @Override
  public void decreaseVolume() {
    System.out.println("Decreasing Volume of Chruch Bell");
  }
}
public class InterfaceDemo {
  public static void main(String[] args) {
    SchoolBell sb = new SchoolBell();
    ChruchBell cb = new ChruchBell();

    // Using the overridden methods
    sb.ring();
    cb.ring();

    // Using the non-abstract methods of Bell class
    sb.increaseVolume();
    cb.decreaseVolume();
  }
}

输出:

Ringing the School bell: ting
Ringing the Chruch Bell: ting
Increasing Volume of School Bell
Decreasing Volume of Chruch Bell

实现接口的抽象类

如上一节所述,我们可以在抽象类中实现接口的方法。下面的代码演示了这一点。

interface Bell {
  String sound = "ting";
  // only abstract methods allowed in interface
  public void ring();
  public void increaseVolume();
  public void decreaseVolume();
}
abstract class AbstractBell implements Bell {
  public void increaseVolume() {
    System.out.println("Increasing Volume");
  }
  public void decreaseVolume() {
    System.out.println("Decreasing Volume");
  }
}

总结

抽象是面向对象编程中使用的最基本的概念之一。抽象用于隐藏实现,只向用户提供最少的基本细节。在 Java 中,抽象是通过使用抽象类或接口来完成的。两者之间的主要区别是抽象类也可以提供部分抽象,而接口将始终提供完整抽象。在本教程中,我们讨论了两者之间的一些主要区别。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便