This answer describing how Spring for Java works instantiates an object of type Interface which is illegal yet it received 21 upvotes. Why is this answer upvoted for illegal behavior?

Spring contains (as Skaffman rightly pointed out) a MVC framework. To explain in short here are my inputs. Spring supports segregation of service layer, web layer and business layer, but what it really does best is "injection" of objects. So to explain that with an example consider the example below:

public interface FourWheel
   public void drive();

public class RoadTrip
    private FourWheel myCarForTrip;
  • 3
    I don't see a instantiation in his answer. – tkausl Jun 12 '16 at 16:49
  • @tkausl private FourWheel myCarForTrip; – Jossie Calderon Jun 12 '16 at 17:00
  • 1
    Thats not a instantiation though... – tkausl Jun 12 '16 at 17:04
  • Did you mean to ask this on Stack Overflow's meta site? We're a software design site, and people can vote however they like. – Robert Harvey Jun 12 '16 at 17:08
  • 2
    @RobertHarvey No, because I wanted to know why the following behavior was accepted by the community, and apparently not illegal (and downvoted). Until a few moments later of research did I realize that I was mistaking declarations and instantiations. – Jossie Calderon Jun 12 '16 at 17:10

What the user performed was a declaration of the interface, but not an instantiation. An instantiation would be as follows:

Fourwheel myFour = new FourWheel();

Declarations of interface types are legal because class types that implement that interface can be assigned to these declarations, shown below:

FourWheel newFour = new Sedan();

Alternatively, an anonymous inner class may be used:

Fourwheel myFour = new FourWheel() { public void say(){ System.out.println("Anonymous inner class");}};
  • What is "anonymous" about that assignment? Isn't Sedan a concrete type? – Robert Harvey Jun 12 '16 at 17:09
  • That's the declaration of a field (or member variable), and Sedan is not anonymous. – bigstones Jun 12 '16 at 17:11
  • 1
    @RobertHarvey I've edited my answer further. This explains why a class is called "anonymous": It implements an interface, has no name, and the implements keyword is not used because it has no name. – Jossie Calderon Jun 12 '16 at 17:31
  • 1
    Apparently the class is considered anonymous because it is nested. Oh, well. Time and again, my C# knowledge fails to illuminate Java; they really are different languages. C# anonymous classes don't have names at all, and they don't have to be nested. – Robert Harvey Jun 12 '16 at 17:35
  • 3
    @RobertHarvey It's not anonymous because it's nested; it's anonymous because it's a nameless. That code snippet creates an instance of a nameless class that implements the FourWheel interface. – Doval Jun 12 '16 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.