43

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 interface and class? Moreover, is Java moving towards multiple inheritance slowly?

soorapadman
  • 4,451
  • 7
  • 35
  • 47
Aakash
  • 2,029
  • 14
  • 22
  • 31
    I'm surprised this has so many upvotes... Remaining differences: `protected` support, `package-private` support, **basically everything but the addition of `private` and `static`**.. Interfaces can't extend classes, the reserved keywords `interface` and `class`, *the philosophy/purpose/reason for existance* of the two.. Could probably keep going.. – Vince Jun 01 '17 at 05:28
  • 3
    The answer still is the same as in [Interface with default methods vs Abstract class in Java 8](https://stackoverflow.com/q/19998454/2711488), except that there can be `private` methods in an interface, which obviously can’t have impact to other classes. – Holger Jun 01 '17 at 13:19
  • 5
    @Aakash **Student**: "*Are there any stupid questions?*" **Professor**: "*Of course not!*" - What if the student were to ask that question again right after they just got the answer? Wouldn't that be a stupid question? The idea behind that phrase is "*don't be afraid to ask questions, it's how you get answers*", don't abuse it to encourage laziness. – Vince Jun 01 '17 at 18:23
  • 1
    @VinceEmigh you have more up-votes then the second most up-voted answer :) you should make you comment into one – Eugene Jun 01 '17 at 18:35
  • *what would be the remaining difference in interface and class* - assuming you know what java classes do, can't you enumerate for yourself what interfaces will still be lacking after those additions? If we take your question at face value then it sounds like you're asking people what features java classes have. – the8472 Jun 02 '17 at 14:08
  • I feel question should be more like does abstract class have any benefits anymore after interface is capable of doing everything. Ofcourse we can't create interface object, have constructor or maintain state. – rashid Jul 20 '17 at 10:40
  • I think the interface private method feature is discussed rather nicely here: https://www.journaldev.com/12850/java-9-private-methods-interfaces – Rann Lifshitz Feb 15 '18 at 10:01
  • @VinceEmigh Apologies for so late comment. In that case, the **student** is stupid, **not** the question. – Aakash Jul 06 '18 at 06:00

5 Answers5

46

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.

If there were a way for an interface to force an implementation to have a particular non-public field or straight-out define one itself, the game would change and interfaces could compete with classes.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • It's not pretty and I don't want to see it in production, but couldn't the state be encapsulated with an inner class to simulate multiple class inheritance? Similar to this: https://stackoverflow.com/questions/3427073/java-interface-implementation-pair/3442218#3442218 – Terran Oct 10 '17 at 20:26
  • Since the inner class as well as the field referencing it would be public, this doesn't really count as inheriting private state. ;) Public state is no problem, just pull the state-relating methods into the interface. – Nicolai Parlog Oct 11 '17 at 06:13
29

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.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
20

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.

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
2

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.

hi.nitish
  • 2,732
  • 2
  • 14
  • 21
2

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.

Umar Tahir
  • 585
  • 6
  • 21