I have searched books read through website but cannot get a justified answer. whats is the proper meaning of composition and how we attain has-a-relationship in the program.
-
3I don't understand what you're asking. Composition means an object is composed of other objects: they *have* other objects, has-a by definition. I don't know how it can be made any more clear. https://en.wikipedia.org/wiki/Object_composition – Dave Newton Apr 24 '19 at 18:27
-
2Possible duplicate of [Java composition (has-a) relationship clarification](https://stackoverflow.com/questions/39947396/java-composition-has-a-relationship-clarification) – Benjamin Urquhart Apr 24 '19 at 18:28
-
Dave Newton, While I agree what you say, but sometimes we need to see the level of the person and answer accordingly. Not everyone is a born english men, some struggle to learn english, we must also consider the age factor of the questioner in mind before being too specific in coming to the conclusion. Thanks – Imran Rafiq Rather Apr 24 '19 at 18:39
-
@ImranRafiqRather There are multitudes of tutorials (in multiple languages) that cover this thoroughly. Even a cursory explanation makes it clear that composition by definition is has-a--IMO there's very little room for confusion here, regardless of "level". YMMV. – Dave Newton Apr 24 '19 at 19:06
1 Answers
Composition:
A composition in literal terms means " using of objects within other objects" like small pieces form a bigger entity.
In programming world, We say a programme is composed of different modules and parts.
let me give you a simple example.
A car has many parts, engine, doors, breaks, headlights etc, Now in a object oriented language, Car would be a separate Object, within it Engine, Doors , Breaks ... will be used to make a car functioning.
Has-A-relation
Now coming to the second part of your question. Has a relationship can be understood by the above example itself.
I will say, Car has-a-engine, Car has-headlight etc. Mobile phone has a screen
So Engine, Headlight and Screen are objects being used in Car and Mobile respectively.
Hope that answers the question. I would advice you to read Head First Java book to learn java step by step. And remember these things are to be related with the world and get more clarity of what they mean.

- 7,677
- 1
- 16
- 35
-
Beenish, have you understood it now, or you need more clarity. I am here to help, Just never stop learning and never quit programming. Take small small steps and you will master it one day. Believe me. – Imran Rafiq Rather Apr 24 '19 at 18:46