Java 中 extends 与implements 的区别 ?

一、介绍extends 与 implements 的概念

  1、之间的继承使用extends : 子类extends父类的属性和方法,并且进行扩展或者重写。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 父类
class Animal {
    public void eat() {
        System.out.println("Animal is eating");
    }
    public void noeat() {
        System.out.println("Animal is noeating");
    }
}
// 子类继承父类Animal
class Dog extends Animal {
    @Override // 覆盖父类的方法的标志,个人认为等于重写
    public void eat() {
        System.out.println("Dog is eating");
    }
}

  Animal(父类) 存在两个方法:eat() 和noeat()。extends 可以不用都实现。但是implements 必须全部实现

   PS:这里补充一个重载和重写的区别:

    重写:方法名、参数名和返回类型一样

    重载:方法名相同,其他比如:参数名、返回类型都可以改变

  2、实现接口的方法采用implements

1
2
3
4
5
6
7
8
9
10
interface Flyable {
    void fly();
}
 
class Bird implements Flyable {
    @Override
    public void fly() {
        System.out.println("Bird is flying");
    }
}

 

posted @   我太想努力了  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示