本文重点介绍了Java类和接口之间的区别。它们在语法上看起来很相似,都包含方法和变量,但它们在很多方面都不同。
班级:
类是用户定义的蓝图或原型,从中创建对象。它表示一种类型的所有对象共有的一组属性或方法。通常,类声明可以按顺序包含这些组件:
- 修饰符:类可以是公共的或具有默认访问权限(有关详细信息,请参阅此处)。
- 类名:名称应以首字母开头(按惯例大写)。
- 超类(如果有):类的父类(超类)的名称,如果有的话,以关键字 extends 开头。一个类只能扩展(子类)一个父类。
- 接口(如果有):类实现的接口的逗号分隔列表(如果有) ,前面是关键字实现。一个类可以实现多个接口。
- 主体:用大括号 { } 包围的类主体。
构造函数用于初始化新对象。字段是提供类及其对象状态的变量,方法用于实现类及其对象的行为。
例子:
Java
// Java program to demonstrate Class
// Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName()
{
return name;
}
// method 2
public String getBreed()
{
return breed;
}
// method 3
public int getAge()
{
return age;
}
// method 4
public String getColor()
{
return color;
}
@Override
public String toString()
{
return ("Hi my name is "
+ this.getName()
+ ".\nMy breed, age and color are "
+ this.getBreed() + ", "
+ this.getAge() + ", "
+ this.getColor());
}
public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy", "papillon",
5, "white");
System.out.println(tuffy.toString());
}
}
Java
// Java program to demonstrate
// working of interface.
import java.io.*;
// A simple interface
interface in1 {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class testClass implements in1 {
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}
// Driver Code
public static void main(String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
输出:
Hi my name is tuffy.
My breed, age and color are papillon, 5, white
界面:
和类一样,接口可以有方法和变量,但接口中声明的方法默认是抽象的(只有方法签名,没有主体)。
- 接口指定类必须做什么而不是如何做。它是班级的蓝图。
- 接口是关于功能的,就像 Player 可能是一个接口,任何实现 Player 的类都必须能够(或必须实现)move()。所以它指定了类必须实现的一组方法。
- 如果一个类实现了一个接口并且没有为接口中指定的所有函数提供方法体,那么必须将类声明为抽象类。
- Java库示例是比较器接口。如果一个类实现了这个接口,那么它就可以用来对一个集合进行排序。
句法 :
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
例子:
Java
// Java program to demonstrate
// working of interface.
import java.io.*;
// A simple interface
interface in1 {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class testClass implements in1 {
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}
// Driver Code
public static void main(String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
输出:
Geek
10
类和接口的区别:
Class | Interface |
---|---|
The keyword used to create a class is “class” | The keyword used to create an interface is “interface” |
A class can be instantiated i.e, objects of a class can be created. | An Interface cannot be instantiated i.e, objects cannot be created. |
Classes does not support multiple inheritance. | Interface supports multiple inheritance. |
It can be inherit another class. | It cannot inherit a class. |
It can be inherited by another class using the keyword ‘extends’. | It can be inherited by a class by using the keyword ‘implements’ and it can be inherited by an interface using the keyword ‘extends’. |
It can contain constructors. | It cannot contain constructors. |
It cannot contain abstract methods. | It contains abstract methods only. |
Variables and methods in a class can be declared using any access specifier(public, private, default, protected) | All variables and methods in a interface are declared as public. |
Variables in a class can be static, final or neither. | All variables are static and final. |