I'm trying to wrap my head around interfaces and I've been reading a lot of similar questions/answers on the topic, but it's still not clicking with me. I understand what an interface literally is, but I don't get why you would ever use it. So an interface contains a bunch of empty functions, and you then implement that interface in a class. But what I don't get is, you still have to write a full function declaration for any function you take from the interface. So say you have a function 'void printHello()' in your interface, and you implement that interface in a class. You still have to write: public void printHello() {      System.out.println(&quot;Hello!&quot;); }  which is a complete function declaration. You could delete the 'implements interface' command and literally nothing would change, you would still have a working function 'printHello()'. So functionally speaking, isn't the interface basically doing nothing? To go further, you could have another class that also implements the same interface, but you could make the same function do something completely different: public void printHello() {      System.out.println(&quot;Bababooey!&quot;); }  So it's not like it's pre-defining functions so you can call them anywhere like a class/object would do. If this is the case what is the interface really providing?
685
I have seen this mentioned a few times and I am not clear on what it means. When and why would you do this?   I know what interfaces do, but the fact I am not clear on this makes me think I am missing out on using them correctly.   Is it just so if you were to do:  IInterface classRef = new ObjectWhatever()   You could use any class that implements IInterface? When would you need to do that? The only thing I can think of is if you have a method and you are unsure of what object will be passed except for it implementing IInterface. I cannot think how often you would need to do that.   Also, how could you write a method that takes in an object that implements an interface? Is that possible?
78505
I'm asking with regards to c#, but I assume its the same in most other languages.  Does anyone have a good definition of expressions and statements and what the differences are?
6406
When verbally talking about methods, I'm never sure whether to use the word argument or parameter or something else. Either way the other people know what I mean, but what's correct, and what's the history of the terms?  I'm a C# programmer, but I also wonder whether people use different terms in different languages.  For the record I'm self-taught without a background in Computer Science. (Please don't tell me to read  because I'm asking this for the benefit of other people who don't already have a copy of 's marvellous book.)  Summary  The general consensus seems to be that it's OK to use these terms interchangeably in a team environment. Except perhaps when you're defining the precise terminology; then you can also use "formal argument/parameter" and "actual argument/parameter" to disambiguate.
49954
In C#, when I have an interface and several concrete implementations, can I cast the interface to a concrete type or is concrete type cast to interface?  What are the rules in this case?
62585
Given the following example, why do I have to explicitly use the statement b-&gt;A::DoSomething() rather than just b-&gt;DoSomething()?  Shouldn't the compiler's overload resolution figure out which method I'm talking about?  I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.)  class A {   public:     int DoSomething() {return 0;}; };  class B : public A {   public:     int DoSomething(int x) {return 1;}; };  int main() {   B* b = new B();   b-&gt;A::DoSomething();    //Why this?   //b-&gt;DoSomething();    //Why not this? (Gives compiler error.)   delete b;   return 0; }
77567
I've read in the  and the  the mention of inner classes. What is their purpose?  I see in the Apex Workbook that they used private variables and setter/getter methods in the inner class, but why didn't they just do that in the outer class with a constructer, getter, and setter?
9451
I've been looking at the JMS API from J2EE and found a strange behavior where certain methods that are declared in an interface (e.g., createQueue in Session) are declared again in the subinterfaces such as QueueSession, and with identical documentation.  Since an subinterface "inherits" all the method declarations of the interface it inherits, and since the JavaDoc tool has no problem sorting out the JavaDocs of the subinterface and creating an "inherited operations" list, I can't figure out what this achieves.  The only think of is that initially the call was in Session, and then moved to QueueSession when the specific subclass was created, though then I would have expected to see something in the documentation of the upperclass. But this is just conjecture.  So the question is: Is there a convincing reason to redeclare a method in a subinterface?
33911
I have been reading through the  and was curious about the  declaration. I personally have never used it, however I am interested in exploring the language.   What is a good example of using friend?    Reading the FAQ a bit longer I like the idea of the &lt;&lt; &gt;&gt; operator overloading and adding as a friend of those classes. However I am not sure how this doesn't break encapsulation. When can these exceptions stay within the strictness that is OOP?
51439
How do you do "inline functions" in C#? I don't think I understand the concept. Are they like anonymous methods? Like lambda functions?  Note: The answers almost entirely deal with the ability to , i.e. "a manual or compiler optimization that replaces a function call site with the body of the callee." If you are interested in , see  or .
5948
A "non-believer" of C# was asking me what the purpose to extension methods was. I explained that you could then add new methods to objects that were already defined, especially when you don't own/control the source to the original object.  He brought up "Why not just add a method to your own class?" We've been going round and round (in a good way). My general response is that it is another tool in the toolbelt, and his response is it is a useless waste of a tool... but I thought I'd get a more "enlightened" answer.  What are some scenarios that you've used extension methods that you couldn't have (or shouldn't have) used a method added on to your own class?
69266
So I believe that I have a real world use case for an object model that uses abstract classes and interface. I do however have a few questions about the best way to go about it:  Interface  public interface iLove{    String pet(); }   Abstract  public abstract class Animal{    public string animalName {get;set;}    public boolean happy {get;set;}     public virtual boolean isHappy(); }   Class  public class cat extends Animal implements iLove{    public cat(String nm){      animalName = nm;   }    public string pet(){     system.debug(animalName);             happy = true;     return 'prrrrrrr';   }    public override boolean isHappy(){     return happy == null ? false : happy;   }  }   So, if I instantiate like this:  Animal a = New Cat('Fred');   I do not have access to the method pet() since Animal does not implement pet  Doing this:  iLove i = (iLove)a;   I can now can execute pet()  However, if I instantiate like so:  iLove i = New cat('Fred');   Executing pet() debugs the animalName and there is no access to isHappy() or animalName directly  Now, the goal is to have many Animals that have a common definition (Animal) that may have different interfaces. for this example each one implements their own pet method, Dog - Bark, Lion - Roar, etc.  Is there a better way to handle all of this where I would not have to Instantiate Animal and then cast to iLove  Goal would be one line for instantiation where I would have access to the properties in Animal as well as the methods of the interface without having to do  Cat c = New Cat('Fred');   As obviously that would work but then it cuts out the whole point of the interface....  Is the casting of the SuperClass to the Interface the way to go about it?  Should I just define the abstract methods in the abstract class. If so what is the point of the Interface. I would suspect in this case it may not be needed but could be used if other classes which to interact with the cat without having to worry about the Animal class.  Thoughts?  Putting the interface on the Animal class has similar effects...
85756
Could someone please demystify interfaces for me or point me to some good examples? I keep seeing interfaces popup here and there, but I haven't ever really been exposed to good explanations of interfaces or when to use them.  I am talking about interfaces in a context of interfaces vs. abstract classes.
14488
I saw this in , in reference to shortcomings of the Java spec:  There are more shortcomings and this is a subtle topic. Check  out: public class methodOverloading{      public static void hello(Integer x){           System.out.println(&quot;Integer&quot;);      }       public static void hello(long x){           System.out.println(&quot;long&quot;);      }       public static void main(String[] args){          int i = 5;          hello(i);      } }  Here &quot;long&quot; would be printed (haven't checked it myself), because the compiler chooses widening over auto-boxing. Be careful when using auto-boxing or don't use it at all!  Are we sure that this is actually an example of widening instead of autoboxing, or is it something else entirely? On my initial scanning, I would agree with the statement that the output would be &quot;long&quot; on the basis of i being declared as a primitive and not an object.  However, if you changed hello(long x)  to hello(Long x)  the output would print &quot;Integer&quot; What's really going on here?  I know nothing about the compilers/bytecode interpreters for java...
62479
I am doing , and am having a few problems with function notation. I understand the form f(x).  What does the form f: x ↦ y mean? Could you also give one or two examples?  And, if possible, state your source. Thank you.
18992
I'm developing an application where I the need to invoke a method of a generic class and I don't care about the instances actual type. Something like the following Java code:  public class Item&lt;T&gt;{   private T item;    public doSomething(){...} }  ... public void processItems(Item&lt;?&gt;[] items){  for(Item&lt;?&gt; item : items)    item.doSomething(); }   At the time I was on a hurry, so I solved my problem by defining a interface with the methods I needed to invoke and made the generic class implement it.  public interface IItem   {    void doSomething(); }  public class Item&lt;T&gt; : IItem {   private T item;    public void doSomething(){...} }  ... public void processItems(IItem[] items) {  foreach(IItem item in items)    item.doSomething(); }   This workaround works fine, but I'd like to know what is the correct way to achieve the same behavior.    EDIT:  I forgot to refer that the caller of processItems doesn't know the actual types. Actually the idea was that the array passed as argument to processItems could contain intermixed types. Since its not possible to have such an array in .Net, using a non generic base class or interface seems to be the only way.
18490
Can someone provide a simple explanation of methods vs. functions in OOP context?
81715
Edit:  Of course my real code doesn't look exactly like this. I tried to write semi-pseudo code to make it more clear of whay I wanted to do.  Looks like it just messed things up instead.  So, what I actually would like to do is this:  Method&lt;Interface1&gt;(); Method&lt;Interface2&gt;(); Method&lt;Interface3&gt;(); ...   Well ... I thought that maybe I could turn it into a loop using reflection. So the question is: How how do I do it. I have very shallow knowledge of reflection. So code examples would be great.  The scenario looks like this:  public void Method&lt;T&gt;() where T : class {} public void AnotherMethod() {     Assembly assembly = Assembly.GetExecutingAssembly();      var interfaces = from i in assembly.GetTypes()     where i.Namespace == "MyNamespace.Interface" // only interfaces stored here     select i;      foreach(var i in interfaces)     {         Method&lt;i&gt;(); // Get compile error here!     }       Original post:  Hi!  I'm trying to loop through all interfaces in a namespace and send them as arguments to a generic method like this:  public void Method&lt;T&gt;() where T : class {} public void AnotherMethod() {     Assembly assembly = Assembly.GetExecutingAssembly();      var interfaces = from i in assembly.GetTypes()     where i.Namespace == "MyNamespace.Interface" // only interfaces stored here     select i;      foreach(var interface in interfaces)     {         Method&lt;interface&gt;(); // Get compile error here!     } }   The error I get is "Type name expected, but local variable name found". If I try  ...     foreach(var interface in interfaces)     {         Method&lt;interface.MakeGenericType()&gt;(); // Still get compile error here!     } }   I get "Cannot apply operator '&lt;' to operands of type 'method group' and 'System.Type'" Any idea on how to get around this problem?