2

Why does java force interface methods to be public and how does abstract class differs from an interface?

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Let me ask counter-question: what other methods would you want interface to support? – Pshemo Apr 07 '16 at 17:24
  • 1
    Otherwise, you wouldn't be able to see what you want the interface to implement when implementing the interface... – ifly6 Apr 07 '16 at 17:25
  • [Interface vs Abstract Class (general OO)](http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo) – Pshemo Apr 07 '16 at 17:26
  • Can you find a good use-case for a *private* method in an interface ? ;) – Nir Alfasi Apr 07 '16 at 17:27
  • An interface states a contract/API, this means that any class that implements the interface should respect the contract (implement the methods declared in the interface, keeping their signatures and etc). A private method is an implementation detail, which is why it shouldn't be part of the contract. The implementation details are up to the class implementors to decide. For that reason, having a private method in an interface beats the purpose of having an interface... – Nir Alfasi Apr 07 '16 at 17:30

1 Answers1

1

A quick response is:

  • All the interface methods must be overriden, for that they must be public.
  • Abstract classes can be extended by other classes, the interfaces only can be implemented by classes and inherited by other interfaces.

You can get a lot more information here: http://www.javaworld.com/article/2077421/learn-java/abstract-classes-vs-interfaces.html

Elvermg
  • 427
  • 6
  • 12