Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
The default access modifier of member in interface is public as we know every one.But one guy ask me that what will happen if we declare access modifier public in interface.
I said it throws compile time error.then he asked me

Why Can't we declare access modifier in interface?
is there any compiler specifications to not declare access modifier in interface
Posted
Comments
PIEBALDconsult 17-Jul-15 0:20am    
Because everything in an Interface has to be public anyway.
Sergey Alexandrovich Kryukov 17-Jul-15 1:23am    
Probably, the inquirer knows that. The question is: why?
—SA
PIEBALDconsult 17-Jul-15 1:27am    
Lack of imagination on the part of the compiler developers?
Sergey Alexandrovich Kryukov 17-Jul-15 1:47am    
Oh, I realized long time ago that imagination is the main thing in programming.
—SA

1 solution

Consider interface as a definition for the outside users, what does functionality in an object look like if I'd call it. Interface describes the 'signature' of methods and properties as others see it.

From Access Modifiers (C# Programming Guide)[^]
"Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members."

Now what interface doesn't define is that it doesn't define how, using what methods, a functionality is built. So it's not a definition, how to implement but how to be used from outside once implemented.

Having that said, it wouldn't make sense to define constraints, what must be implemented inside an interface since that's not it's purpose

An example:
Think about a digital thermometer. It shows you the temperature, it may have a functionality to remember the highest and the lowest values, those values may be reset using a button and perhaps you can change the scale between Fahrenheit and Celsius. This is you interface, how to use it.

What you don't see is how the thermometer is built internally what components are used, how is the wiring done and so on. More importantly those do not matter since if the manufacturer makes the same model year after year, they may find more applicable components and the internal design may change. Perhaps some wiring is taken away and replaced by more advanced chip etc.

All these changes in inner parts have no effect on how to use the thermometer. The interface stays the same even if the internal implementation may change dramatically.

From the user point of view, you're not interested how the thermometer is built, only how you use it. Because of that the inner logic may not be defined in the definition of the interface :)
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 17-Jul-15 1:22am    
Good points, a 5.
—SA
Wendelius 17-Jul-15 1:29am    
Thanks SA!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900