接口和抽象类的区别
Posted lanjianblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口和抽象类的区别相关的知识,希望对你有一定的参考价值。
- 接口的方法默认是public abstract,所有方法在接口中不能有实现,抽象类可以有非抽象的方法。(从 Java 8 开始,接口也可以拥有默认的方法实现,这是因为不支持默认方法的接口的维护成本太高了。在 Java 8 之前,如果一个接口想要添加新的方法,那么要修改所有实现了该接口的类。)
- 接口中的实例变量默认是private final static类型的,而抽象类中则不一定。
- 一个类可以实现多个接口,但最多只能继承一个抽象类。
- 一个类实现接口的话要实现接口的所有方法,而抽象类不一定。
- 接口中不能有构造函数,而抽象类中可以有构造函数。
- 从设计层面来说,抽象是对类的抽象,是一种模板设计,是IS-A的关系,而接口是行为的抽象,是一种行为的规范,是LIKE-A的关系。
!--5f39ae17-8c62-4a45-bc43-b32064c9388a:W3siYmxvY2tJZCI6IjI4NDctMTU0Nzk2NzA2MTg1MSIsImJsb2NrVHlwZSI6Imxpc3QtaXRlbSIsInN0eWxlcyI6eyJhbGlnbiI6ImxlZnQiLCJpbmRlbnQiOjAsInRleHQtaW5kZW50IjowLCJsaW5lLWhlaWdodCI6MS43NSwiYmFjay1jb2xvciI6IiIsInBhZGRpbmciOiIifSwidHlwZSI6Imxpc3QtaXRlbSIsInJpY2hUZXh0Ijp7ImRhdGEiOlt7ImNoYXIiOiLmjqUifSx7ImNoYXIiOiLlj6MifSx7ImNoYXIiOiLnmoQifSx7ImNoYXIiOiLmlrkifSx7ImNoYXIiOiLms5UifSx7ImNoYXIiOiLpu5gifSx7ImNoYXIiOiLorqQifSx7ImNoYXIiOiLmmK8ifSx7ImNoYXIiOiJwIn0seyJjaGFyIjoidSJ9LHsiY2hhciI6ImIifSx7ImNoYXIiOiJsIn0seyJjaGFyIjoiaSJ9LHsiY2hhciI6ImMifSx7ImNoYXIiOiIgIn0seyJjaGFyIjoiYSJ9LHsiY2hhciI6ImIifSx7ImNoYXIiOiJzIn0seyJjaGFyIjoidCJ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Ijoi57O7In0seyJjaGFyIjoi77yMIn0seyJjaGFyIjoi6ICMIn0seyJjaGFyIjoi5o6lIn0seyJjaGFyIjoi5Y+jIn0seyJjaGFyIjoi5pivIn0seyJjaGFyIjoi6KGMIn0seyJjaGFyIjoi5Li6In0seyJjaGFyIjoi55qEIn0seyJjaGFyIjoi5oq9In0seyJjaGFyIjoi6LGhIn0seyJjaGFyIjoi77yMIn0seyJjaGFyIjoi5pivIn0seyJjaGFyIjoi5LiAIn0seyJjaGFyIjoi56eNIn0seyJjaGFyIjoi6KGMIn0seyJjaGFyIjoi5Li6In0seyJjaGFyIjoi55qEIn0seyJjaGFyIjoi6KeEIn0seyJjaGFyIjoi6IyDIn0seyJjaGFyIjoi77yMIn0seyJjaGFyIjoi5pivIn0seyJjaGFyIjoiTCJ9LHsiY2hhciI6IkkifSx7ImNoYXIiOiJLIn0seyJjaGFyIjoiRSJ9LHsiY2hhciI6Ii0ifSx7ImNoYXIiOiJBIn0seyJjaGFyIjoi55qEIn0seyJjaGFyIjoi5YWzIn0seyJjaGFyIjoi57O7In0seyJjaGFyIjoi44CCIn1dLCJpc1JpY2hUZXh0Ijp0cnVlLCJrZWVwTGluZUJyZWFrIjp0cnVlfSwibGV2ZWwiOjEsImluZGV4Ijo2LCJsaXN0SWQiOiI4OTY2LTE1NDc5NjcwNzk4NDEiLCJsaXN0VHlwZSI6Im9yZGVyZWQifV0=-->以上是关于接口和抽象类的区别的主要内容,如果未能解决你的问题,请参考以下文章
接口和抽象类的区别
接口和抽象类的区别
接口和抽象类的区别
接口和抽象类的区别是什么?
接口和抽象类的区别是什么?
接口和抽象类的区别