例如:
在下面的示例中,Student类(子类)继承了People类(超类)的属性和方法:
class People {
  protected String name = "cjavapy";       // People 属性
  public void study() {                    // People 方法
    System.out.println("好好学习");
  }
}
class Student extends People {
  private String className = "Python";    // Student 属性
  public static void main(String[] args) {
    // 创建 student 对象
    Student student = new Student();
    // 调用student的study() 方法 (从 People 类继承) student 
    student.study();
    // 显示name属性(从 People 类继承)的值和Student类的className的值
    System.out.println(student.name + " " + student.className);
  }
} 1、定义和用法
extends关键字扩展了一个类(表示一个类是从另一个类继承的)。
在Java中,可以将属性和方法从一个类继承到另一个类。 继承分为两类:
- 子类(子类)-从另一个类继承的类
- 超类(父级)-继承自的类
要从类继承,需要使用extends关键字。