• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding inheritance v/s composition

 
shweta sahib
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java so i have some basic doubts like could you please explain me what is the difference between inheritance and composition and please could you explain me through example .When to use the inheritance and when composition. Thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you use both when they make sense.

Inheritance is used for "IS-A" relationships. a car IS-A vehicle, so it makes sense for the car class to extend the vehicle class.

Composition is used for "HAS-A" relationship. a car HAS-A motor. It would not make sense for a care to extend the motor class.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Shweta.

You have inherited the characteristics/genes from your parents.

You are composed of bones , muscle, body parts just like any other human being.

Makes sense now?
 
shweta sahib
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you explain through code snippets.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shweta sahib wrote:could you explain through code snippets.


Please SearchFirst(←click).

inheritance vs composition example
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are concepts of how your classes / interfaces are related to each other.

Composition
: For example, if I have a Building, it is but obvious that the building will have rooms. Correct? In other words, I can say that the BUILDING---> has ---> ROOMS. Or I can be more specific and say the building is composed of rooms. This is the concept of composition in Java. When an object is composed of many other sub-objects, it is said to be composed of those objects.

Inheritance : We see animals all around us. A dog, cat, rat, cow etc. are all Animals. In other words A dog --> is a --> Animal, a Rat--> is a --> Animal etc. Now an Animal will sure have certain common behaviors like walk, run, jump, eat etc. All these behaviors are defined using the concept of inheritance. Every Animal will walk, jump, run etc , BUT, in its own manner. In other words, I can say that a Dog has inherited walk behavior from Animal class.

could you explain through code snippets.


Since you are new to Java, I would advise you to approach learning not the "how"/"coding" part but the "what"/"why" parts first. You must understand what they mean and the need for having these concepts in the first place.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: . . .
You have inherited the characteristics/genes from your parents.
. . .
The computer science version of inheritance is very different from genetic inheritance. It is even more different from the concept that you inherited money from your parents.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mansukhdeep Thind wrote: . . .
You have inherited the characteristics/genes from your parents.
. . .
The computer science version of inheritance is very different from genetic inheritance. It is even more different from the concept that you inherited money from your parents.


Well, inheritance in Java also has to do with inheriting behavior. Isn't that similar to inheriting genes from parent that define one's behavior?

For the money part though , that depends whether your Parent class has made the money private of not

 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: . . . Well, inheritance in Java also has to do with inheriting behavior. Isn't that similar to inheriting genes from parent that define one's behavior?
. . .
No. It is impossible for a person to grow up with a gene which their parents did not possess. Java inheritance permits additional fields, methods, etc, which the superclasses do not have. Obviously one has to be careful about adding fields and methods.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was ignoring the possibility of mutations in genes.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I was ignoring the possibility of mutations in genes.


That is how it is similar to inheritance in Java. My genes will be similar to my mum and dad but different from either of them.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really is best if we just drop the analogy to biological inheritance. It's tenuous at best, and when you start getting into details, it falls apart fast. Unfortunately, beginners get locked into confusion along the lines of "biological inheritance works this way, therefore so must OO inheritance, so why isn't Java operating correctly?" The more we push people toward associating OO inheritance with biological inheritance, the more we set them up for confusion down the line.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shweta sahib wrote:could you explain through code snippets.

I fail to see how this will help

 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: . . . That is how it is similar to inheritance in Java. My genes will be similar to my mum and dad but different from either of them.
Unless you have been ill and picked up mutations, your genes will be identical to your parents’, which differs from computer science. You are not identical to all your parents’ genes. Only some of them. As Jeff says, that is where the analogy falls down and becomes confusion.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mansukhdeep Thind wrote: . . . That is how it is similar to inheritance in Java. My genes will be similar to my mum and dad but different from either of them.
Unless you have been ill and picked up mutations, your genes will be identical to your parents’, which differs from computer science. You are not identical to all your parents’ genes. Only some of them. As Jeff says, that is where the analogy falls down and becomes confusion.


Hmm.. Lets wait for Shweta to respond and let us know what she has (or rather has not) understood. And if she has, then what is it exactly that she has understood about inheritance and composition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic