-4

In Java: Scope of the function(s) declared in Interface is strictly public? whereas, Scope of the function(s) declared in Abstract class may be default, protected or public!!!

We know that both providing same problem description(As per my question above) i.e. The Concrete class which inherit it have to define the function(s).

So, why different rules for both?

5 Answers5

2

First of all, what you are talking about is "access" not "scope". It is important to get the terminology correct. Scope has a different meaning in Java.

An abstract class is really just like a normal class with normal implementation details (state variables and code) ... except that some of the implementation is left out; i.e. the abstract methods. So naturally you want the full repertoire of class language features.

A (pre-Java 8) interface is different1. Now there is no state and no code, just a "contract" that any implementing class must fulfill.

Now that doesn't completely address this question, but for the rest, I refer you to this Q&A - Protected in Interfaces - which asks why you can't use protected in an interface. As you can see, there isn't a single convincing answer. Rather there are multiple answers proposed, each of which could be valid to a lesser or greater extent. The only way to get the real answer(s) would be to ask James Gosling et al. Ultimately, it was a design choice made early in the life of the Java language.

So, why different rules for both?

Because the purpose of each of the two constructs is different.


1 - With Java 8, we can now declare default methods in an interface. In other words, there can be code in an interface now, albeit code that is common to all classes that implement the interface.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Interface defines the external, therefore public, interface of the class. When you use an interface you need not to know anything about how things are done internally.

An abstract class on the other just states what is missing to have a working implementation. For example, a pretty common pattern is:

abstract class x {

   public void SomeTask() {
        // ...
        doSomeTask();
   }

   protected abstract void doSomeTask();
}
Exceptyon
  • 1,584
  • 16
  • 23
0

Because an interface is by definition a public interface. It makes no sense do define a non-public method(!) on an interface.

It makes perfect sense to define a private method on an abstract class.

0

An interface specifies how to interact with a type. Therefore, everything must be public, since it must all be accessible from outside the class implementing it. An abstract class however, can contain protected, private or public because it can have implementation. It can call its own protected method, which will be implemented by a subclass.

Chris Wohlert
  • 610
  • 3
  • 12
0

Methods defined in abstract class can be executed in the same class. Interface doesn't execute any code, so there is no point in creating private of protected method. Interfaces are meant to be public, they are APIs to implementation.

agilob
  • 6,082
  • 3
  • 33
  • 49