迹忆客 专注技术分享

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

Java 中 抽象类 和 接口的不同点

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

众所周知,抽象是指隐藏功能的内部实现,只向用户显示功能。 例如:只显示它是做什么的,隐藏其如何实现的。 抽象类on)和接口都用于抽象,因此接口和抽象类是抽象的必需先决条件

 

Java 抽象类vs接口
Java 抽象类vs接口

 

  • 方法类型 :接口只能有抽象方法。抽象类可以有抽象方法和非抽象方法。从 Java 8 开始,它也可以有默认和静态方法。
  • 最终变量 :Java 接口中声明的变量默认为最终变量。抽象类可能包含非最终变量。
  • 变量类型 :抽象类可以有 final、非final、静态和非静态变量。接口只有静态和 final 变量。
  • 实现 :抽象类可以提供接口的实现。接口不能提供抽象类的实现。
  • 继承与抽象 :Java 接口可以使用关键字“implements”来实现,抽象类可以使用关键字“extends”来继承。
  • 多个实现 :一个接口只能继承另一个Java接口,一个抽象类可以继承另一个Java类并实现多个Java接口。
  • 数据成员的可访问性 :默认情况下,Java 接口的成员是 public 的。 Java 抽象类可以具有 private、protected 等类成员。

下面我们来看一个示例

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 + " "
                           + "has been moved to"
                           + " 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("Rectangle has been drawn ");
    }
 
    @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("Circle has been drawn ");
    }
 
    @Override public double area()
    {
        return (double)((pi * radius * radius));
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        Shape rect = new Rectangle(2, 3, "Rectangle");
 
        System.out.println("Area of rectangle: "
                           + rect.area());
 
        rect.moveTo(1, 2);
 
        System.out.println(" ");
 
        Shape circle = new Circle(2, "Circle");
 
        System.out.println("Area of circle: "
                           + circle.area());
 
        circle.moveTo(2, 4);
    }
}

运行示例

上面的示例输出结果如下

Area of rectangle: 6.0
Rectangle has been moved to x = 1 and y = 2
 
Area of circle: 12.56
Circle has been moved to x = 2 and 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("Rectangle has been drawn ");
    }
 
    @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("Circle has been drawn ");
    }
 
    @Override public double area()
    {
 
        return (double)((pi * radius * radius));
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        Shape rect = new Rectangle(2, 3);
 
        System.out.println("Area of rectangle: "
                           + rect.area());
 
        Shape circle = new Circle(2);
 
        System.out.println("Area of circle: "
                           + circle.area());
    }
}

运行示例

上面示例编译执行结果如下

Area of rectangle: 6.0
Area of circle: 12.56

什么时候用接口,什么时候用抽象类?

如果下面中的任何一个适用于你的情况,考虑使用抽象类:

  • 在java应用程序中,有一些相关的类需要共享一些代码,那么可以将这些代码行放在抽象类中,这个抽象类应该由所有这些相关类继承。
  • 可以在抽象类中定义非静态或非final字段,以便通过方法访问和修改它们所属对象的状态。
  • 可以在继承抽象类的类中设置许多 public 方法或字段,或者需要公共以外的访问修饰符(例如受保护和私有)。

如果下面中的任何一个适用于你的情况,考虑使用接口:

  • 它是完全抽象的,接口中声明的所有方法都必须由实现该接口的类来实现。
  • 一个类可以实现多个接口。它被称为多重继承。
  • 想指定特定数据类型的行为,但不关心谁实现了它的行为。

上一篇:没有了

下一篇:如何在 Java 中遍历 LinkedList 实例?

转载请发邮件至 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

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便