Java继承

1. 类的继承

当一个类继承另一个类时,使用extends关键字。子类可以访问超类(父类)中的公有(public)和受保护(protected)成员

class Parent {  
    void greet() {  
        System.out.println("Hello from Parent class");  
    }  
}  

class Child extends Parent {  
    void greetChild() {  
        System.out.println("Hello from Child class");  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Child child = new Child();  
        child.greet();      // 调用父类方法  
        child.greetChild(); // 调用子类方法  
    }  
}

2. 抽象类继承

如果子类从一个抽象类继承,需要实现所有的抽象方法,除非子类本身也是抽象类。

abstract class Animal {  
    abstract void makeSound();  
}  

class Dog extends Animal {  
    @Override  
    void makeSound() {  
        System.out.println("Woof!");  
    }  
}

3. 接口的实现

在Java中,接口使用implements关键字来实现,而不是extends。但是,接口之间可以使用extends来继承其他接口

interface Animal {  
    void makeSound();  
}  

interface Pet extends Animal {  
    void play();  
}  

class Dog implements Pet {  
    public void makeSound() {  
        System.out.println("Woof!");  
    }  
    
    public void play() {  
        System.out.println("Dog is playing!");  
    }  
}

在这里特别说一下接口:



接口的基本特征

  1. 方法的声明:接口中的方法默认是publicabstract,即使不显式地写出这些修饰符。
  2. 常量:接口中可以定义常量,这些常量默认是public static final的。
  3. 默认方法(自Java 8起):接口可以包含带有实现的方法,称为默认方法。
  4. 静态方法(自Java 8起):接口可以包含静态方法,允许直接通过接口调用。
  5. 多继承:一个类可以实现多个接口,从而支持多继承。

接口的定义和使用

1. 定义接口

class Dog implements Animal {  
    // 实现抽象方法  
    @Override  
    public void makeSound() {  
        System.out.println("Woof!");  
    }  
}  

class Cat implements Animal {  
    @Override  
    public void makeSound() {  
        System.out.println("Meow!");  
    }  
}

2. 实现接口

一个类可以通过implements关键字来实现接口,并必须实现接口中的所有抽象方法。

class Dog implements Animal {  
    // 实现抽象方法  
    @Override  
    public void makeSound() {  
        System.out.println("Woof!");  
    }  
}  

class Cat implements Animal {  
    @Override  
    public void makeSound() {  
        System.out.println("Meow!");  
    }  
}

3. 使用接口

通过创建实现了特定接口的类的实例可以调用接口的方法。

public class Main {  
    public static void main(String[] args) {  
        Animal myDog = new Dog();  
        Animal myCat = new Cat();  
        
        myDog.makeSound(); // 输出: Woof!  
        myCat.makeSound(); // 输出: Meow!  

        myDog.eat();       // 输出: This animal eats food.  
        Animal.describe(); // 输出: Animals are living beings that move.  
    }  
}

 

4. 多继承接口

一个类可以实现多个接口

interface CanFly {  
    void fly();  
}  

class Bird implements Animal, CanFly {  
    @Override  
    public void makeSound() {  
        System.out.println("Chirp!");  
    }  

    @Override  
    public void fly() {  
        System.out.println("Bird is flying.");  
    }  
}

 

5. 注意事项

  • 抽象类与接口的区别:抽象类可以有状态(字段),而接口不能。接口是完全抽象的,通常用于定义一种能力或行为。
  • 实现多个接口:Java支持一个类实现多个接口,但只允许单继承,即一个类只能继承一个父类。
  • 变量的访问:接口中的变量是public static final(常量),不能在实现类中重新赋值。
  • 接口的应用:接口常用于设计模式、API定义和回调机制等场景,以实现更灵活的代码模块。

6. 示例 – 完整程序

下面是一个更全面的示例,展示了接口的用法:

 

interface Vehicle {  
    void start();  
    void stop();  
}  

class Car implements Vehicle {  
    @Override  
    public void start() {  
        System.out.println("Car is starting.");  
    }  

    @Override  
    public void stop() {  
        System.out.println("Car has stopped.");  
    }  
}  

class Bicycle implements Vehicle {  
    @Override  
    public void start() {  
        System.out.println("Bicycle is starting.");  
    }  

    @Override  
    public void stop() {  
        System.out.println("Bicycle has stopped.");  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Vehicle myCar = new Car();  
        Vehicle myBicycle = new Bicycle();  
        
        myCar.start();         // 输出: Car is starting.  
        myCar.stop();          // 输出: Car has stopped.  
        
        myBicycle.start();     // 输出: Bicycle is starting.  
        myBicycle.stop();      // 输出: Bicycle has stopped.  
    }  
}


4. 多级继承

Java支持多级继承,即一个类可以直接继承另一个类,该类可以再继承一个类,依此类推。

class Grandparent {  
    void greet() {  
        System.out.println("Hello from Grandparent class");  
    }  
}  

class Parent extends Grandparent {  
    void greetParent() {  
        System.out.println("Hello from Parent class");  
    }  
}  

class Child extends Parent {  
    void greetChild() {  
        System.out.println("Hello from Child class");  
    }  
}

5. 注意事项

  • 单继承:Java不支持多重类继承,一个类只能直接继承一个父类。
  • 构造函数:子类的构造函数必须调用父类的构造函数,可以使用super()来显式调用。
  • 方法重写:子类可以重写父类的方法,提供具体实现。
  • 访问权限:父类的private成员在子类中不可访问。

6. 示例 – 完整程序

下面是一个更完整的示例,展示了extends的用法:

class Vehicle {  
    void start() {  
        System.out.println("Vehicle is starting");  
    }  
}  

class Car extends Vehicle {  
    void start() {  
        System.out.println("Car is starting");  
    }  

    void drive() {  
        System.out.println("Car is driving");  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Vehicle myCar = new Car();  
        myCar.start(); // 输出: Car is starting  
        // myCar.drive(); // 编译错误,因为 Vehicle 类没有 drive 方法  
    }  
}

总结

  • 使用 extends 时,你在构建一个从父类继承的类结构。
  • 使用 implements 时,你在构建一个符合某种接口的类,并承诺实现该接口的所有方法。

因此,在Java编程时,选择使用 extends 还是 implements 主要取决于你的设计需求和类之间的关系。

  • 18
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值