Java 继承
extends 关键字、super 的用法、继承的特性与构造方法调用顺序。
继承的概念
Inheritance lets a subclass reuse the parent's fields and methods, modelling an is-a relationship. Java uses extends, and every class ultimately inherits Object.
class Animal {
void eat() {
System.out.println("eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking");
}
}
public class InheritDemo {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
System.out.println(d instanceof Animal);
}
}eating
barking
true继承的特性
A subclass inherits all non-private members, may add new ones and may override methods. A final class cannot be extended and a final method cannot be overridden.
- Single inheritance — one parent class only
- Multilevel — A extends B extends C is fine
- No multiple class inheritance — use interfaces instead
- Constructors are not inherited
extends 关键字
class Child extends Parent declares inheritance. Constructing a child runs the parent constructor first, then the child's, so the parent part is fully initialised before use.
class A {
A() {
System.out.println("A()");
}
}
class B extends A {
B() {
super();
System.out.println("B()");
}
}
public class Order {
public static void main(String[] args) {
new B();
}
}A()
B()super 关键字
super.method() calls the overridden parent version, super.field reads a parent field, and super(...) invokes the parent constructor — it must be the first statement.
class Person {
String name = "person";
void hello() {
System.out.println("hello from Person");
}
}
class Student extends Person {
String name = "student";
@Override
void hello() {
super.hello();
System.out.println("hello from Student");
}
void show() {
System.out.println(name);
System.out.println(super.name);
}
}
public class SuperDemo {
public static void main(String[] args) {
Student s = new Student();
s.hello();
s.show();
}
}hello from Person
hello from Student
student
personimplements 与继承的区别
extends inherits one class (is-a); implements fulfils interfaces, several at once (can-do). They combine as class A extends B implements C, D.
interface Swimmer {
void swim();
}
class Bird {
void fly() {
System.out.println("flying");
}
}
class Duck extends Bird implements Swimmer {
@Override
public void swim() {
System.out.println("swimming");
}
}
public class Both {
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
System.out.println(d instanceof Bird);
System.out.println(d instanceof Swimmer);
}
}flying
swimming
true
true继承的概念
继承让子类获得父类的字段和方法,实现代码复用和「is-a」关系。Java 用 extends 表示继承,所有类最终都继承 Object。
class Animal {
void eat() {
System.out.println("eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking");
}
}
public class InheritDemo {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
System.out.println(d instanceof Animal);
}
}eating
barking
true继承的特性
子类拥有父类非 private 的成员;子类可以扩展新成员;子类可以重写父类方法。final 类不能被继承,final 方法不能被重写。
- 单继承 —— 只能有一个父类
- 多层继承 —— A 继承 B、B 继承 C 可以
- 不支持多继承类 —— 用接口替代
- 构造方法不会被继承
extends 关键字
class 子类 extends 父类 声明继承。创建子类对象时,先执行父类构造方法、再执行子类构造方法,保证父类部分先初始化完成。
class A {
A() {
System.out.println("A()");
}
}
class B extends A {
B() {
super();
System.out.println("B()");
}
}
public class Order {
public static void main(String[] args) {
new B();
}
}A()
B()super 关键字
super.方法() 调用父类被重写的版本,super.字段 访问父类字段,super(...) 调用父类构造方法且必须是子类构造的第一句。
class Person {
String name = "person";
void hello() {
System.out.println("hello from Person");
}
}
class Student extends Person {
String name = "student";
@Override
void hello() {
super.hello();
System.out.println("hello from Student");
}
void show() {
System.out.println(name);
System.out.println(super.name);
}
}
public class SuperDemo {
public static void main(String[] args) {
Student s = new Student();
s.hello();
s.show();
}
}hello from Person
hello from Student
student
personimplements 与继承的区别
extends 是「继承父类」,只能一个,表示 is-a;implements 是「实现接口」,可以多个,表示 can-do。两者常一起用:class A extends B implements C, D。
interface Swimmer {
void swim();
}
class Bird {
void fly() {
System.out.println("flying");
}
}
class Duck extends Bird implements Swimmer {
@Override
public void swim() {
System.out.println("swimming");
}
}
public class Both {
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
System.out.println(d instanceof Bird);
System.out.println(d instanceof Swimmer);
}
}flying
swimming
true
true