Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between abstract classes and interfaces in Java

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要为大家展示了"Java中抽象类和接口有哪些区别",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java中抽象类和接口有哪些区别"这篇文章吧。

1、抽象类 vs 接口

方法类型: 接口只能有抽象方法。抽象类可以有抽象和非抽象方法。从 Java 8 开始,它也可以有默认和静态方法。

最终变量: 在 Java 接口中声明的变量默认是最终的。抽象类可能包含非最终变量。

变量类型: 抽象类可以有final、non-final、静态和非静态变量。接口只有静态和最终变量。

实现: 抽象类可以提供接口的实现。接口不能提供抽象类的实现。

继承 vs 抽象: Java 接口可以使用关键字"implements"来实现,抽象类可以使用关键字"extends"进行扩展。

多重实现: 一个接口只能扩展另一个Java接口,一个抽象类可以扩展另一个Java类并实现多个Java接口。

数据成员的可访问性: 默认情况下,Java 接口的成员是公共的。Java 抽象类可以具有私有、受保护等类成员。

import java.io.*;abstract class Shape { String objectName = " "; Shape(String name) { this.objectName = name; } public void moveTo(int x, int y){ System.out.println(this.objectName + " " + "已移至" + " x = " + x + " and y = " + y); } abstract public double area(); abstract public void draw();}class Rectangle extends Shape { int length, width; Rectangle(int length, int width, String name){ super(name); this.length = length; this.width = width; } @Override public void draw(){ System.out.println("矩形已绘制"); } @Override public double area(){ return (double)(length * width); }}class Circle extends Shape { double pi = 3.14; int radius; Circle(int radius, String name){ super(name); this.radius = radius; } @Override public void draw(){ System.out.println("圆形已绘制"); } @Override public double area(){ return (double)((pi * radius * radius) / 2); }}class HY { public static void main(String[] args){ Shape rect = new Rectangle(2, 3, "Rectangle"); System.out.println("矩形面积:" + rect.area()); rect.moveTo(1, 2); System.out.println(" "); Shape circle = new Circle(2, "Circle"); System.out.println("圆的面积:" + circle.area()); circle.moveTo(2, 4); }}

输出:

矩形面积:6.0

矩形已移至 x = 1 和 y = 2

圆的面积:6.28

圆已移至 x = 2 和 y = 4

如果我们在矩形和圆形之间没有任何通用代码,请使用界面。

import java.io.*;interface Shape { void draw(); double area();}class Rectangle implements Shape { int length, width; Rectangle(int length, int width){ this.length = length; this.width = width; } @Override public void draw(){ System.out.println("矩形已绘制"); } @Override public double area(){ return (double)(length * width); }}class Circle implements Shape { double pi = 3.14; int radius; Circle(int radius) { this.radius = radius; } @Override public void draw(){ System.out.println("圆形已绘制"); } @Override public double area(){ return (double)((pi * radius * radius) / 2); }}class HY { public static void main(String[] args){ Shape rect = new Rectangle(2, 3); System.out.println("矩形面积:" + rect.area()); Shape circle = new Circle(2); System.out.println("圆的面积:" + circle.area()); }}

输出:

矩形面积:6.0

圆的面积:6.28

什么时候用什么?

如果以下任何陈述适用于您的情况,请考虑使用抽象类:

在java应用程序中,有一些相关的类需要共享一些代码行,那么你可以将这些代码行放在抽象类中,并且这个抽象类应该由所有这些相关类进行扩展。

您可以在抽象类中定义非静态或非最终字段,以便您可以通过方法访问和修改它们所属的对象的状态。

您可以期望扩展抽象类的类具有许多公共方法或字段,或者需要除 public 之外的访问修饰符(例如 protected 和 private)。

如果以下任何陈述适用于您的情况,请考虑使用接口:

它是一个完全的抽象,接口中声明的所有方法都必须由实现此接口的类来实现。

一个类可以实现多个接口。它被称为多重继承。

您想指定特定数据类型的行为,但不关心谁实现其行为。

以上是"Java中抽象类和接口有哪些区别"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report