Abstract classes and Multiple Inheritance

前端 未结 5 1337
南笙
南笙 2021-01-19 15:02

We can achieve the same functionality as interfaces by using abstract classes, So why java doesn\'t allow the following code?

abstract class Animals
{
    pu         


        
5条回答
  •  别那么骄傲
    2021-01-19 15:49

    This is not allowed because you can do more than this with abstract classes. It wouldn't make sense to allow multiple inheritance, provided you only used an abstract class when you could have used an interface.

    It is simpler to only use abstract classes for things you can't do with an interface, in which case you wouldn't be able to use two abstract parent classes.

    Note: with Java 8 there is less you can't do with an interface, you can have public instance and static methods with implementations.

    In Java 9 you will be able to have private methods in interfaces ;)

提交回复
热议问题