For Java!! We can implement different methods in different classes. In interface we create an abstract method  and provide its implementation in the class that implements the particular interface. What is the purpose of creating an interface if we can create and implement methods in classes separately? Plz help me I'm new in Java?
9009
So lets say I have this interface:  public interface IBox {    public void setSize(int size);    public int getSize();    public int getArea();   //...and so on }   And I have a class that implements it:  public class Rectangle implements IBox {    private int size;    //Methods here }   If I wanted to use the interface IBox, i can't actually create an instance of it, in the way:  public static void main(String args[]) {     Ibox myBox=new Ibox(); }   right? So I'd actually have to do this:  public static void main(String args[]) {     Rectangle myBox=new Rectangle(); }   If that's true, then the only purpose of interfaces is to make sure that the class which implements an interface has got the correct methods in it as described by an interface? Or is there any other use of interfaces?
28147
What is reflection, and why is it useful?  I'm particularly interested in Java, but I assume the principles are the same in any language.
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?
16584
would report a violation for:  ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();   The violation was "Avoid using implementation types like 'ArrayList'; use the interface instead".  The following line would correct the violation:  List&lt;Object&gt; list = new ArrayList&lt;Object&gt;();   Why should the latter with List be used instead of ArrayList?
22377
Some time ago, I came across a piece of code, that used some piece of standard Java functionality to locate the classes that implemented a given interface. I know the functions were hidden in some non-logical place, but they could be used for other classes as the package name implied. Back then I did not need it, so I forgot about it, but now I do, and I can't seem to find the functions again. Where can these functions be found?  Edit: I'm not looking for any IDE functions or anything, but rather something that can be executed within the Java application.
24648
What are the differences in implementing interfaces implicitly and explicitly in C#?  When should you use implicit and when should you use explicit?  Are there any pros and/or cons to one or the other?    Microsoft's official guidelines (from first edition ) states that using explicit implementations are not recommended, since it gives the code unexpected behaviour.  I think this guideline is very valid in a pre-IoC-time, when you don't pass things around as interfaces.  Could anyone touch on that aspect as well?
99846
I need to determine if a Class object representing an interface extends another interface, ie:   package a.b.c.d;     public Interface IMyInterface extends a.b.d.c.ISomeOtherInterface{     }   according to  Class.getSuperClass() will return null for an Interface.       If this Class represents either the   Object class, an interface, a   primitive type, or void, then null is   returned.   Therefore the following won't work.  Class interface = Class.ForName("a.b.c.d.IMyInterface") Class extendedInterface = interface.getSuperClass(); if(extendedInterface.getName().equals("a.b.d.c.ISomeOtherInterface")){     //do whatever here }   any ideas?
87364
EDIT: As of Java 8, static methods are now allowed in interfaces.  Here's the example:  public interface IXMLizable&lt;T&gt; {   static T newInstanceFromXML(Element e);   Element toXMLElement(); }   Of course this won't work.  But why not?  One of the possible issues would be, what happens when you call:  IXMLizable.newInstanceFromXML(e);   In this case, I think it should just call an empty method (i.e. {}).  All subclasses would be forced to implement the static method, so they'd all be fine when calling the static method.  So why isn't this possible?  EDIT: I guess I'm looking for answer that's deeper than "because that's the way Java is".   Is there a particular technological reason why static methods can't be overwritten? That is, why did the designers of Java decide to make instance methods overrideable but not static methods?  EDIT: The problem with my design is I'm trying to use interfaces to enforce a coding convention.  That is, the goal of the interface is twofold:   I want the IXMLizable interface to allow me to convert classes that implement it to XML elements (using polymorphism, works fine). If someone wants to make a new instance of a class that implements the IXMLizable interface, they will always know that there will be a newInstanceFromXML(Element e) static constructor.   Is there any other way to ensure this, other than just putting a comment in the interface?
6832
How does one go about and try to find all subclasses of a given class (or all implementors of a given interface) in Java? As of now, I have a method to do this, but I find it quite inefficient (to say the least). The method is:    Get a list of all class names that exist on the class path Load each class and test to see if it is a subclass or implementor of the desired class or interface   In Eclipse, there is a nice feature called the Type Hierarchy that manages to show this quite efficiently. How does one go about and do it programmatically?
25516
I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view?
97180
I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.  I understand there may be several conventions and habits and think about this question as an aggregator of the best techniques people use dealing with private methods in Objective-C.   Please include an argument for your approach when posting it. Why is it good? Which drawbacks does it have (that you know of) and how you deal with them?    As for my findings so far.  It is possible to use  [e.g. MyClass (Private)] defined in MyClass.m file to group private methods.  This approach has 2 issues:   Xcode (and compiler?) does not check if you define all methods in private category in corresponding @implementation block You have to put @interface declaring your private category in the begin of MyClass.m file, otherwise Xcode complains with a message like "self may not respond to message "privateFoo".   The first issue can be worked around with  [e.g. MyClass ()]. The second one bothers me a lot. I'd like to see private methods implemented (and defined) near the end of the file; I do not know if that's possible.
48229
what about using "this" with methods in Java? Is it optional or there are situations when one needs to use it obligatory?  The only situation I have encountered is when in the class you invoke a method within a method. But it is optional. Here is a silly example just to show what I mean:  public class Test {      String s;      private String hey() {         return s;     }      public String getS(){         String sm = this.hey();         // here I could just write hey(); without this         return sm;     } }
1841
What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of Java?
103027
In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java?
32250
I've looked at , specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example?    What I can make from the answers so far, it seems to me to be just a more complex way of doing this:  have an abstract class: MoveAlong with a virtual method: DoIt() have class Car inherit from MoveAlong,       implementing DoIt() { ..start-car-and-drive..} have class HorseCart inherit from MoveAlong,       implementing DoIt() { ..hit-horse..} have class Bicycle inherit from MoveAlong,       implementing DoIt() { ..pedal..} now I can call any function taking MoveAlong as parm  passing any of the three classes and call DoIt Isn't this what Strategy intents? (just simpler?)   [Edit-update] The function I refer to above is replaced with another class in which MoveAlong would be attribute which is set according to need based on the algorithm implemented in this new class. (Similar to what is demonstrated in the accepted answer.)    [Edit-update] Conclusion  The Strategy Pattern has it's uses, but I am a strong believer in KISS, and would tend to more straightforward and less obfuscatory techniques. Mostly since I want to pass on easily maintainable code (and 'cos I'll most likely be the one who have to make the changes!).
6666
I've read that it is possible to add a method to an existing object (i.e., not in the class definition) in Python.   I understand that it's not always good to do so. But how might one do this?
69502
Possible Duplicate:          Would someone explain the differences between these two beasts and how to use them. AFAIK, many  pre.2.0 classes were implemented without generic types, thus causing latter version to implement both flavors of interfaces. Is the the only case why one would need to use them?  Can you also explain in depth how to use them.?  Thanks