views:

252

answers:

4

Why I can't able to specify static type of methods in interface.

Is there any alternative to do this??

but I should use only inerface insted of abstract class.

/

Is there any problem in specifing acess specifier in interface?

I want to specify the events in Interface and that should be acessed only by implemented class so i want protected acess specifier to that event.

and I have certain function that can be make use of interface in another class in that case i can use public acess specifier.

A: 

interfaces only describe your implementator to the public. The only access modifier for an interface is "internal" on the interface itself, which makes it an intertface only visible to all member of the same assembly or the ones made known with the "InternalsVisibleTo" attribute.

If only implementators should see a certain event, then that event would have to be in some kind of base class.

A static method is pretty much a remnant of procedural programming. they do have their use (see e.g. extension methods) but really haven't got much to do with OOP.

flq
A: 

I'm at loss on what you mean with the first question, but your second question about using protected in interfaces is a straight "no". Interfaces are called "interfaces" because they specify the "interface" (i.e. public methods) in which you can communicate with that object.

thr
+2  A: 

I don't know what language you're taking about but I'll answer as if in C#.

Why I can't able to specify static type of methods in interface. Is there any alternative to do this??

It is because you can't override static methods. What are you trying to achieve?

Members in interfaces are always public in C#. If you need to have other protection levels, use an abstract class. What would the purpose of protected events be if you can't access them from the interface? It has nothing to do with the interface then (remember, the interface can't have any code). If you are referring to that only implementing classes can raise the events, rest assured that they are the only ones that can raise them. Events are built that way - only the class itself can raise events. You can't raise an event external from the class (except if you have a method on the class, raising the event).

Statement
A: 

My experience with OOP is in relation to Actionscript 3, but I am sure it's relative.

Static methods cannot be overidden and they do not get used when a class extends the ancestor class.

Access modifiers aren't used in the interface because the interface is used to setup PUBLIC methods.

In AS3 an interface is merely a method signature with no block, but including data-types.

It is important to note that getter/setter method signatures can be used in an interface as well. These are useful and if you only use a setter you have created a read-only public access to a private encapsulated variable.

As far as interface instead of an abstract class, it all depends on how you want to implement your code. Often times and interface and an abstract class are used together to achieve different results utilizing inheritance and polymorphism (extends / implements).

Brian Hodge hodgedev.com blog.hodgedev.com

Brian Hodge