继承有哪些特点?

  1. 子类通过extends关键字继承父类

  1. 继承只是单继承(Java doesn’t support multiple inheritance),
    继承只是单继承原因:目的是为了让类变得更加的安全,如果继承了两个的话,调用父类方法,如果两个类都有这个方法,不知道继承的哪一个。可以通过"传递"的方式实现多继承的效果,后续还有多实现的方式
  2. 每一个类都有它继承的类,如果没写默认继承Object,如果写了extends就继承extend后面的类,每个类都会直接或间接的继承Object
  3. We can’t extend Final classes in java

  1. 子类可以调用父类中的成员
    (1)public protected权限修饰符修饰的属性方法可以当做自己的来使用。
    (2)private members of superclass are not directly accessible to subclass.but it can be indirectly accessible via getter and setter methods.
    (3)构造方法严格意义来说,不算作子类继承过来的,只是子类在调用构造方法时默认调用父类的构造方法
    (4)程序块严格意义来说,不算作子类继承过来的,程序块没有名字,子类不能直接调。子类执行构造方法之,默认调用了父类的构造方法,父类调用构造方法之前自动执行父类的块

  2. 子类继承的父类的方法如果不能满足子类的需求,可以在子类中覆盖父类该方法
    We can override the method of Superclass in the Subclass. However we should always annotate overridden method with @Override annotation.

  3. 子类可以添加自己独有的属性和方法


  1. We can create an instance of subclass and then assign it to superclass variable, this is called upcasting

  2. When an instance of Superclass is assigned to a Subclass variable, then it’s called downcasting

  3. We can use instanceof instruction to check the inheritance between objects.


  1. Code reuse is the most important benefit of inheritance because subclasses inherits the variables and methods of superclass.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值