以下案例基于jdk17
1. 一个子类只能继承一个抽象类, 但能实现多个接口
// 抽象父类
abstract class Person {
}
// 接口
interface Game {
}
// 接口
interface Work {
}
// 子类只能继承一个父类,但是可以实现多个接口
public class Student extends Person implements Game, Work{
}
2. 抽象类可以有构造方法, 接口没有构造方法
// 抽象父类
abstract class Person {
public Person() {
System.out.println("抽象类可以有构造方法!");
}
}
// 接口
interface Game {
// 接口不能拥有构造方法
}
// 接口
interface Work {
}
// 子类只能继承一个父类,但是可以实现多个接口
public class Student extends Person implements Game, Work{
}
3. 抽象类可以有任意成员变量, 接口只能拥有 public static final 的成员变量
// 抽象父类
abstract class Person {
// 普通的成员变量,可以添加任何关键字修饰(static,private,final...)
String name;
int age;
public Person() {
System.out.println("抽象类可以有构造方法!");
}
}
// 接口
interface Game {
// 接口中的所有成员变量默认都是 public static final
// 不写也是 public static final
String name = "王者荣耀";
public static final int price = 6;
// 接口不能拥有构造方法
}
// 接口
interface Work {
}
// 子类只能继承一个父类,但是可以实现多个接口
public class Student extends Person implements Game, Work{
}
4. 抽象类可以没有抽象方法, 接口也可以有普通方法
接口在JDK8之前都是抽象方法,在JDK8可以有default方法,在JDK9中允许有私有普通方法
// 抽象父类
abstract class Person {
// 普通的成员变量,可以添加任何关键字修饰
String name;
int age;
public Person() {
System.out.println("抽象类可以有构造方法!");
}
// 可以拥有各种访问限制的普通方法
public void show() {
System.out.println("public show");
}
protected void eat() {
System.out.println("protected eat");
}
private void drink() {
System.out.println("private drink");
}
public static void WC() {
System.out.println("public static void WC()");
}
// 可以拥有抽象方法
public abstract void salary();
}
// 接口
interface Game {
// 接口中的所有成员变量默认都是 public static final
String name = "王者荣耀";
public static final int price = 6;
// 接口不能拥有构造方法
// 可以拥有抽象方法
public abstract void grade();
// 默认都是 public abstract
void score();
// 可以拥有 default 方法,给实现类使用或重写
default void play() {
System.out.println("default void play()");
}
// 可以拥有 private 方法,供自己使用
private void study() {
System.out.println("private void study()");
}
}
// 接口
interface Work {
}
// 子类只能继承一个父类,但是可以实现多个接口
public class Student extends Person implements Game, Work{
// 子类必须重写父类和接口的抽象方法
// 其它的看自己的要求
@Override
public void salary() {
}
@Override
public void grade() {
}
@Override
public void score() {
}
}
5. 抽象类和接口都可以有静态方法,但接口类的静态方法不能被实现类的对象调用
// 抽象父类
abstract class Person {
// 普通的成员变量,可以添加任何关键字修饰
String name;
int age;
public Person() {
System.out.println("抽象类可以有构造方法!");
}
// 可以拥有各种访问限制的普通方法
public void show() {
System.out.println("public show");
}
protected void eat() {
System.out.println("protected eat");
}
private void drink() {
System.out.println("private drink");
}
// 可以拥有静态方法,能被子类调用
public static void WC() {
System.out.println("public static void WC()");
System.out.println("子类可以使用");
}
// 可以拥有抽象方法
public abstract void salary();
}
// 接口
interface Game {
// 接口中的所有成员变量默认都是 public static final
String name = "王者荣耀";
public static final int price = 6;
// 接口不能拥有构造方法
// 可以拥有抽象方法
public abstract void grade();
// 默认都是 public abstract
void score();
// 可以拥有 default 方法,给实现类使用或重写
default void play() {
System.out.println("default void play()");
}
// 可以拥有 private 方法,供自己使用
private void study() {
System.out.println("private void study()");
}
// 可以拥有 static 方法
public static void work() {
System.out.println("public static void work()");
System.out.println("只能自己调用可以使用");
}
}
// 接口
interface Work {
}
// 子类只能继承一个父类,但是可以实现多个接口
public class Student extends Person implements Game, Work{
public static void main(String[] args) {
// 可以调用 Person 的静态方法
Student.WC();
// 只能自己调用接口的方法
Game.work();
// 报错
// Static method may be invoked on containing interface class only
// Student.work();
}
// 子类必须重写父类和接口的抽象方法
// 其它的看自己的要求
@Override
public void salary() {
}
@Override
public void grade() {
}
@Override
public void score() {
}
}