0

Why did java make interface allow only public abstract methods?

Why are interface methods always public and not allow protected. Where in abstract class can implement protected abstract methods.

Abstract class can make lower access abstract methods right? An interface is an abstract data type that defines a list of abstract.

Can someone explain to me why it was implemented like that?

public abstract class Animal{
    protected abstract void printName(); 

}

---Assume As Separate file ---

public class Lion extends Animal{

    protected void printName(){}

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Java jansen
  • 187
  • 2
  • 15
  • 3
    For your first question, interfaces are contracts of _behavior_ which a class may have. They say _what_ a class can do, but now _how_ it will do it. So, all methods interfaces are abstract. Actually, that sort of changed in Java 8, but in general what I said is true. – Tim Biegeleisen Oct 05 '18 at 06:43
  • Possible duplicate of [Why do we need interfaces in Java?](https://stackoverflow.com/questions/3528420/why-do-we-need-interfaces-in-java) – deHaar Oct 05 '18 at 06:43
  • @deHaar the question is relatively different – Java jansen Oct 05 '18 at 06:44
  • 1
    `public ` because interfaces are meant to expose behavior to outside world. `abstract ` because the implementation should not be exposed. – The Scientific Method Oct 05 '18 at 06:44
  • @TimBiegeleisen you mean interfaces are contracts of behavior which a class must have? Implementing class should implement all methods of interface right? I thought Interface was relatively derived from abstract class, but now only it doesn't have implementations at all and purely abstract. but abstract class can allow protected abstract methods. – Java jansen Oct 05 '18 at 06:51
  • Yes, what you said is true, so what is your question? – Tim Biegeleisen Oct 05 '18 at 06:52
  • why is interface methods always public and not allow protected. where in abstract class can implement protected abstract methods or even lower – Java jansen Oct 05 '18 at 06:53
  • You may want to revise your Java knowledge, since Java 8, interfaces allow default method implementations. – Mark Rotteveel Oct 12 '18 at 16:13

1 Answers1

2

This answers the question which eventually popped up in your comment:

why is interface methods always public and not allow protected. where in abstract class can implement protected abstract methods or even lower

It doesn't make sense to make an abstract method in an interface anything other than public, because then it wouldn't be possible for an implementing class to see it. Actually, in Java 9, there is such a thing as private interface methods. But, private interface methods cannot also be abstract, because these two modifiers mean different things. Private methods in Java 9 interfaces are intended to be consumed within the interface, e.g. by default methods. So it makes sense to have a private interface method in this case, because it is only intended to be used internally.

Here is a link to a useful blog post on this topic.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360