2

I am confused with these implementations .In an interview,interviewer asked me what is composition and I gave him typical definition then I wrote this part of code for him.

public class Foo {
    private Bar bar = new Bar(); 
}

But he claimed this implementation is correct

interface IFoo
{
    int DoSomthing();
}

class Bar : IFoo
{
    public int DoSomthing()
    {
        throw new NotImplementedException();
    }
}

Which one is correct?

E_N_Y
  • 167
  • 1
  • 2
  • 9
  • Second one is just a class implementing an interface. I don't see composition there. This should help https://www.geeksforgeeks.org/association-composition-aggregation-java/ – Sriram Sakthivel Apr 17 '18 at 16:29
  • 3
    I'm not the person who downvoted you, but it may have been because you don't show that you took any effort to research the composition-over-inheritance principle before asking a question here. Your first code example doesn't have anything to do with composition. – levininja Apr 17 '18 at 16:32
  • Read [this page](https://en.wikipedia.org/wiki/Composition_over_inheritance), the first paragraph and the first example will probably make it click. – levininja Apr 17 '18 at 16:33

2 Answers2

3

Edit: I realize now that both you and your interviewer were correct; answer updated accordingly.

From the wikipedia page on Composition over inheritance:

Composition over inheritance...is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.

Polymorphism is

the provision of a single interface to entities of different types.

So what you did (having Bar be a property of Foo) is Composition because Bar has an instance of Foo through having it as a property.

What your interviewer did was also Composition because, through the interface IFoo, Bar implements the same functionality, and it didn't use inheritance to do so. This appears to be the way it's documented on the linked wiki page but doesn't mean your way is wrong either.

Which method you use for implementing the same functionality in different places would depend on whether it makes sense for Bar to be a property of Foo or not.

levininja
  • 3,118
  • 5
  • 26
  • 41
  • so,you mean he was right?so why in many websites they use the code I wrote in that interview.could you provide some peace of code – E_N_Y Apr 17 '18 at 16:51
  • @E_N_Y give me a link and I'll look into it. – levininja Apr 17 '18 at 16:53
  • [What is the difference between association, aggregation and composition?](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) – E_N_Y Apr 17 '18 at 16:56
  • So next time I should describe both cases! – E_N_Y Apr 17 '18 at 17:10
2

Composition denotes a "is-a-part-of" relationship between objects. For example,

class Engine
{
    //....
}

class Car
{
    Engine engine = new Engine();
    //.....
}

we can see, Engine is-a-part-of Car. Composition and inheritance are two different concepts and you probably shouldn't accept a job offer where he would be your boss. :)

PmanAce
  • 4,000
  • 2
  • 24
  • 29
  • 1
    In your example Car doesn't actually implement Engine, it just has a property of type Engine. The [composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance) principle is about ways to introduce polymorphic behavior, so in other words where Car implements things from Engine. – levininja Apr 17 '18 at 16:51
  • 1
    I am realizing that your method is actually an example of composition as well (and have updated my answer accordingly) but SO isn't letting my remove my downvote from your answer unless the answer is edited. – levininja Apr 17 '18 at 17:05
  • 1
    @levininja just edited it! – PmanAce Apr 17 '18 at 17:15