Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do interface members have no access modifier? [duplicate]

Tags:

Possible Duplicate:
Why can't I have protected interface members?

as title, in C#. Is there no possibility that someone might want to have a protected or an internal interface?

like image 952
Louis Rhys Avatar asked May 18 '11 06:05

Louis Rhys


People also ask

Why are there no access modifiers on the methods in an interface?

Interfaces are Coding contracts, this is the very reason it won't allow any access modifier other then Public in it's Method signatures. But an Interface by itself can be Internal but not private or protected, Internal allows access within the assembly which is perfectly fine.

CAN interface have access modifiers?

Note: Nested interfaces and classes can have all access modifiers. Note: We cannot declare class/interface with private or protected access modifiers.

What is the default access modifier of the members of an interface can I declare any of the members of an interface Public explicitly why?

internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared public , internal , or private . Class members, including nested classes and structs, can be public , protected internal , protected , internal , private protected , or private .

CAN interface have access modifiers in Java?

Access modifiers define the access privileges of classes, interfaces, constructors, methods, and data members. Access modifiers consist of public, private , and protected . If no modifier is present, the default access of package-private is used. Table 8-2 provides details on visibility when access modifiers are used.


1 Answers

Because Interface is in crude terms 'a view to the outside world' and since it is for the outside world, there is no point making its members protected or private.

Or in other words, it is a contract with the outside world which specifies that class implementing this interface does a certain set of things. So, hiding some part of it doesn't make sense.

However, interfaces themselves can have access specifiers like protected or internal etc. Thus limiting 'the outside world' to a subset of 'the whole outside world'.

like image 164
Aamir Avatar answered Sep 28 '22 17:09

Aamir