The different between extends and implements

In Java, `extend` and `implement` are keywords used for inheritance and interface implementation, respectively. Here's the difference between the two:
1. `extend`:
   - Used to inherit from a class.
   - A class can extend only one class (single inheritance).
   - The subclass inherits all the public and protected methods and fields of the superclass.
   - The subclass can add new methods and fields, and also override the methods of the superclass.
   Example:

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Dog barks");
    }
}


2. `implement`:
   - Used to implement an interface.
   - A class can implement multiple interfaces.
   - The implementing class must provide implementations for all the methods declared in the interface.
   - Interfaces can only have method signatures and constants (Java 8 and earlier). From Java 9 onwards, interfaces can have private methods and default methods (methods with a default implementation).
   Example:

interface Animal {
    void makeSound();
}

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Dog barks");
    }
}


In summary, `extend` is used for class inheritance, while `implement` is used for implementing interfaces.
 

### Java 中 `extends` 和 `implements` 的关键区别 #### 1. **基本概念** - `extends` 关键字用于类之间的继承关系,允许子类继承父类的属性和方法[^1]。这种机制使得代码复用成为可能,并支持面向对象编程中的多态特性。 - `interfaces` 则是由一组抽象方法组成的契约,而 `implements` 关键字则用来让一个类实现这些接口所定义的行为[^3]。 #### 2. **单继承 vs 多实现** - 在 Java 中,一个类只能通过 `extends` 继承自单一的一个父类[^4]。这是为了防止多重继承带来的复杂性和潜在冲突问题。 - 而对于接口而言,一个类可以通过 `implements` 同时实现多个接口[^1]。这提供了更大的灵活性,使开发者能够模拟某些形式上的“多重继承”。 #### 3. **成员特征** - 当使用 `extends` 进行继承时,默认情况下会获得父类的所有非私有字段以及公开/受保护的方法(包括静态与实例级别的)。需要注意的是,如果父类中有未实现的抽象方法,则当前类要么也得声明成抽象类,要么提供具体的实现逻辑[^4]。 - 对于 `implements` 所涉及的接口来说,除了默认方法(default methods)之外,其余所有方法均需由该类自行完成实际编码工作[^3]。此外,接口本身并不携带任何状态数据——即不允许存在常规变量,除非它们是以常量的形式存在的(public static final)[^1]。 #### 4. **设计哲学差异** - 使用 `extends` 表达了一种"is-a"(是什么)的关系模型。例如,“狗是一种动物”,因此 Dog 类可能会 extend Animal 类。 - 反之,采用 `implements` 更倾向于描述一种"can-do"(能做什么)的能力关联模式。“鸟可以飞”意味着 Bird 类应当 implement Flyable 接口。 以下是基于上述理论的实际应用例子: ```java // 定义一个基础类 Shape class Shape { void draw() { System.out.println("Drawing a shape..."); } } // Circle 继承自 Shape (is-a relationship) class Circle extends Shape { @Override void draw() { System.out.println("Drawing a circle..."); } } // 定义一个接口 Drawable interface Drawable { default void erase() { System.out.println("Erasing something..."); } void paint(); } // Square 不仅继承了 Rectangle,还实现了 Drawable(can-do relationship) class Square extends Rectangle implements Drawable { @Override public void paint() { System.out.println("Painting a square..."); } } ``` 以上展示了如何利用这两种方式构建复杂的类型体系结构。 --- ### 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PABL01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值