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

This question already has answers here:
Closed 11 years ago.
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?

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'.

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.

All the interface methods are Public. You can't create an access modifier in interface. If you want to use one, use Abstract class.

This is due to the nature of the interface. An interface, by definition is a specification.
A rule in .NET specifications dictates that a class that implements an interface will have to implement all members of that interface.
Now if we mark a member private, then the implementing class cannot implement that particular member.

Please see Non Public Members for C# Interfaces

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.

Related

Difference between abstract Class and Interface in C# [duplicate]

This question already has answers here:
Interface vs Base class
(38 answers)
Interface or abstract class?
(15 answers)
Closed 7 years ago.
this article says
Codeproject
Abstract Class -can have method Declaration and method definition.
But this article says
programcall
Abstract - can have only method Declaration.
I got confused. Can anybody clear me what is the exact difference of both ?
See this article:
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined
As far as I know, abstract classes can already implement some methods or variable's values, as long as it does not implement them all (which would make it a normal class).
An interface cannot do this; it can only provide method and variable stubs.
An interface is more of a contract - it details what methods or properties will be found on an object that implements it.
An abstract class is a base object - it can contain methods, variables and behaviour - however you cannot create a concrete implementation of it - you must inherit from it and extend its behaviour.

Can we create derived public type from private base type in .net?

I know the question sound somewhat stupid, but i have this scenario.
Context
I am creating 2 background tasks (METRO apps, winRT) which have much parts in common.
Now what i want is, a common base class defining common functionality, and 2 task to be derived from base class.
Problem: Background task should be targetted WinMD, and in this mode, it is not possible to create non sealed public class. If i create sealed, i cannot derive from it, and if i create private, i cannot expose the derived types because base is private.
Concern: What are the ways to achieve the desired functionality. (derived public types from private base). One way could be use of Composition (instead of inheritance)
You simply can't. .NET doesn't allow you to make a derived type more accessible than the base type. You'll have to expose the base type.
Note that if you only give it internal constructors, then other code won't be able to instantiate it or create their own derived types... what's the problem with exposing it at that point?
EDIT: Okay, I've just reread the question, and seen this:
it is not possible to create non sealed public class
So:
Could you make all the types involved internal?
Could you make it a public abstract class? (Does WinMD allow that?)
(If you have any other documentation around what's allowed in WinMD, that would be useful.)
If you're allowed to export interfaces, perhaps you could have your base class and derived classes internal, but make them implement public interfaces. Then have public static methods in a public static class to create instances of these internal types and return them via the interface. Again, I don't know if this is allowed - if you have any links to documentation around what's allowed in WinMD, that would be useful for those of us who haven't done any WinRT development.
Yes, you can hide it in the derived class, then expose the base.Property, i.e.,
new public ScrollBar HorizontalScrollBar
{
get { return base.HorizontalScrollBar; }
}

ActionScript3 interface implementation without public accessor?

In C#, it's possible implementing interface methods without making implementing method as public. For example,
void ITest.SomeMethod()
{
// ...
}
Is there equivalent for ActionScript3?
Nope. From the AS3 Language Spec:
Classes that implement an interface method must use the public attribute to implement all interface methods.
In ActionScript, there is no way to add access level qualifiers; however, this question has been asked here, leveraging inheritance of interfaces:
How to expose a method in an interface without making it public to all classes
Perhaps an internal class may be another approach; although, not recommended.
But directly no, all members of ActionScript interfaces are public.

Java2C# translation: public methods in Interfaces in C#

Another translation question, this may be more theoretical, but I am curious as to the design choice. SFNQ:
Why does C# not allow controlling for controlling access to methods in interfaces like Java does? For example, in a C# interface:
public void Visit(Axiom axiom);
Thank you.
In C#, and .Net in general, all methods on an interface are public by default. There is no way to restrict their access.
Consider the alternative, what would it mean to have a protected member on an interface? How would you establish the access rules to allow or disallow a caller of an interface access to the particular method? (I mean protected in the C# sense, not the java one).
Even better, what would private mean?
In both C# and Java, all methods on an interface are public.
In Java, the public keyword is allowed, likely to save on parsing rules. In C#, the public keyword was considered redundant and was removed from interface declarations altogether.
In C# all members of an interface must be public, therefore it will not allow you to add any visibility modifiers to the member declarations. The public keyword is therefore redundant and not needed (infact if you include it you'll get a compiler error).
An interface is a contract which states that you will provide all of the functionlity specified in the interface definition. If you were allowed to have private members in an interface you would not be exposing that functionality (and you would therefore violate the contract).

microsoft C# .net Interface doubts

How can an access specifier to the member of interface be specified?
We can use interface as in two ways
Inheritance ( IsA relation )
Containment in another class (Has A relation).
In this way of implementation
protected access specifier is applied only to the events which are in inheritance relationship (IsA).
public access specifier is applied to the properties which are generally used as Has A relation (containment).
thz..
dinesh..
All interface members are automatically public.
If inheritance and protected members are your goal, inherit from a base class instead.
If composition is your goal, use interfaces.
Interface members have the same access operator as the interface they're in, that's the point of having an interface. Otherwise you'd have a public interface IFoo, which has an internal member Bar, which would be problematic if code wants to program against IFoo: it can't always access Bar, although it can use IFoo: the type implementing IFoo apparently doesn't implement Bar at that point.
So if you want to have some elements internal for example, use an internal interface for those members.

Categories