Java 9 Interface vs Class

前端 未结 5 991
春和景丽
春和景丽 2020-12-08 02:11

As Java 9 is going to allow us to define private and private static methods too in interfaces, what would be the remaining difference in inte

相关标签:
5条回答
  • 2020-12-08 02:38

    Java 9 interfaces still cannot contain fields and constructors. This makes a huge difference between classes and interfaces, so Java 9 is far from multiple inheritance.

    0 讨论(0)
  • 2020-12-08 02:43

    Although its an old question let me give my input on it as well :)

    1. abstract class: Inside abstract class we can declare instance variables, which are required to the child class

      Interface: Inside interface every variables is always public static and final we cannot declare instance variables

    2. abstract class: Abstract class can talk about state of object

      Interface: Interface can never talk about state of object

    3. abstract class: Inside Abstract class we can declare constructors

      Interface: Inside interface we cannot declare constructors as purpose of
      constructors is to initialize instance variables. So what is the need of constructor there if we cannot have instance variables in interfaces.

    4. abstract class: Inside abstract class we can declare instance and static blocks

      Interface: Interfaces cannot have instance and static blocks.

    5. abstract class: Abstract class cannot refer lambda expression

      Interfaces: Interfaces with single abstract method can refer lambda expression

    6. abstract class: Inside abstract class we can override OBJECT CLASS methods

      Interfaces: We cannot override OBJECT CLASS methods inside interfaces.

    I will end on the note that:

    Default method concepts/static method concepts in interface came just to save implementation classes but not to provide meaningful useful implementation. Default methods/static methods are kind of dummy implementation, "if you want you can use them or you can override them (in case of default methods) in implementation class" Thus saving us from implementing new methods in implementation classes whenever new methods in interfaces are added. Therefore interfaces can never be equal to abstract classes.

    0 讨论(0)
  • 2020-12-08 02:52

    Private interface methods in Java 9 behave exactly like other private methods: They must have a body (even in abstract classes) and can neither be called nor overridden by subclasses. As such they do not really interact with inheritance. Talking of which (and particularly multiple inheritance), there are (at least?) three kinds of it:

    • Inheritance of types means that one type can be another type, e.g. String is an Object. Java allowed multiple inheritance of types from day one (via interfaces).
    • Inheritance of behavior means that one type can inherit the behavior of another type. Before Java 8, only classes could implement methods, so there was only single inheritance of this kind. With Java 8 came default methods, which allowed interfaces to implement methods, thus giving Java multiple inheritance of behavior.
    • Inheritance of state means that a type inherits another type's internal state (i.e. fields). As it stands (Java 9 and everything currently proposed for future Java versions), only classes can have state, so there is only single inheritance of this kind.

    As you can see private interface methods do not add anything here.

    Regarding your question of how interfaces and classes compare, there are two main differences: multiple inheritance and state. Interfaces support the former, classes can have the latter. Since state is kind-of important in typical OOP, classes will remain relevant.

    0 讨论(0)
  • 2020-12-08 02:57

    Private methods are not inherited by subclasses, so this feature doesn't affect implementation classes. I believe the private methods in interfaces allow us to share code between default methods.

    Java interfaces still cannot have non-static members. That's a big difference and not multiple inheritance IMO.

    0 讨论(0)
  • 2020-12-08 03:00

    Java Interface in version 9 have private methods but static private. The feature has been introduced to allow modular methods. One function should work with one responsibility instead of using lengthy default methods. It has nothing to do with multiple Inheritance. The more private static methods, the more you will be able to write the clean and reusable code. Anyways, static methods whether public or protected can not be overridden.

    0 讨论(0)
提交回复
热议问题