更新時間:2022-08-03 10:50:40 來源:動力節點 瀏覽1632次
Java抽象類和接口的區別是什么?動力節點小編來告訴大家。眾所周知,抽象是指隱藏功能的內部實現,只向用戶顯示功能。即它的工作原理(顯示),它是如何工作的(隱藏)。抽象類和接口都用于抽象,因此接口和抽象類是必需的先決條件。
方法類型:接口只能有抽象方法。抽象類可以有抽象方法和非抽象方法。從 Java 8 開始,它也可以有默認和靜態方法。
最終變量: Java 接口中聲明的變量默認為最終變量。抽象類可能包含非最終變量。
變量類型:抽象類可以有最終、非最終、靜態和非靜態變量。該接口只有靜態和最終變量。
實現:抽象類可以提供接口的實現。接口不能提供抽象類的實現。
繼承與抽象: Java 接口可以使用關鍵字“implements”來實現,抽象類可以使用關鍵字“extends”進行擴展。
多種實現:一個接口可以擴展一個或多個Java接口,一個抽象類可以擴展另一個Java類并實現多個Java接口。
數據成員的可訪問性:默認情況下,Java 接口的成員是公共的。Java 抽象類可以具有私有、受保護等類成員。
示例 1-A:
// Java Program to Illustrate Concept of
// Abstract Class
// Importing required classes
import java.io.*;
// Class 1
// Helper abstract class
abstract class Shape {
// Declare fields
String objectName = " ";
// Constructor of this class
Shape(String name) { this.objectName = name; }
// Method
// Non-abstract methods
// Having as default implementation
public void moveTo(int x, int y)
{
System.out.println(this.objectName + " "
+ "has been moved to"
+ " x = " + x + " and y = " + y);
}
// Method 2
// Abstract methods which will be
// implemented by its subclass(es)
abstract public double area();
abstract public void draw();
}
// Class 2
// Helper class extending Class 1
class Rectangle extends Shape {
// Attributes of rectangle
int length, width;
// Constructor
Rectangle(int length, int width, String name)
{
// Super keyword refers to current instance itself
super(name);
// this keyword refers to current instance itself
this.length = length;
this.width = width;
}
// Method 1
// To draw rectangle
@Override public void draw()
{
System.out.println("Rectangle has been drawn ");
}
// Method 2
// To compute rectangle area
@Override public double area()
{
// Length * Breadth
return (double)(length * width);
}
}
// Class 3
// Helper class extending Class 1
class Circle extends Shape {
// Attributes of a Circle
double pi = 3.14;
int radius;
// Constructor
Circle(int radius, String name)
{
// Super keyword refers to parent class
super(name);
// This keyword refers to current instance itself
this.radius = radius;
}
// Method 1
// To draw circle
@Override public void draw()
{
// Print statement
System.out.println("Circle has been drawn ");
}
// Method 2
// To compute circle area
@Override public double area()
{
return (double)((pi * radius * radius));
}
}
// Class 4
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating the Object of Rectangle class
// and using shape class reference.
Shape rect = new Rectangle(2, 3, "Rectangle");
System.out.println("Area of rectangle: "
+ rect.area());
rect.moveTo(1, 2);
System.out.println(" ");
// Creating the Objects of circle class
Shape circle = new Circle(2, "Circle");
System.out.println("Area of circle: "
+ circle.area());
circle.moveTo(2, 4);
}
}
輸出
矩形面積:6.0
矩形已移至 x = 1 和 y = 2
圓形面積:12.56
圓圈已移至 x = 2 和 y = 4
如果我們在矩形和圓形之間沒有任何共同的代碼怎么辦,然后使用界面。
示例 1-B:
// Java Program to Illustrate Concept of Interface
// Importing I/O classes
import java.io.*;
// Interface
interface Shape {
// Abstract method
void draw();
double area();
}
// Class 1
// Helper class
class Rectangle implements Shape {
int length, width;
// constructor
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 2
// Helper class
class Circle implements Shape {
double pi = 3.14;
int radius;
// constructor
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));
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating the Object of Rectangle class
// and using shape interface reference.
Shape rect = new Rectangle(2, 3);
System.out.println("Area of rectangle: "
+ rect.area());
// Creating the Objects of circle class
Shape circle = new Circle(2);
System.out.println("Area of circle: "
+ circle.area());
}
}
輸出
矩形面積:6.0
圓形面積:12.56
如果這些陳述中的任何一個適用于您的情況,請考慮使用抽象類:
在java應用程序中,有一些相關的類需要共享一些代碼行,那么您可以將這些代碼行放在抽象類中,這個抽象類應該由所有這些相關類擴展。
您可以在抽象類中定義非靜態或非最終字段,以便通過方法訪問和修改它們所屬對象的狀態。
您可以期望擴展抽象類的類具有許多公共方法或字段,或者需要公共以外的訪問修飾符(例如受保護和私有)。
如果這些陳述中的任何一個適用于您的情況,請考慮使用接口:
它是完全抽象的,接口中聲明的所有方法都必須由實現該接口的類來實現。
一個類可以實現多個Java接口。它被稱為多重繼承。
您想指定特定數據類型的行為,但不關心誰實現了它的行為。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習