aspose file tools*
The moose likes Beginning Java and the fly likes doubt regarding inheritance v/s composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt regarding inheritance v/s composition" Watch "doubt regarding inheritance v/s composition" New topic
Author

doubt regarding inheritance v/s composition

shweta sahib
Greenhorn

Joined: Mar 16, 2013
Posts: 7
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
shweta sahib
Greenhorn

Joined: Mar 16, 2013
Posts: 7
could you explain through code snippets.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

shweta sahib wrote:could you explain through code snippets.


Please SearchFirst(←click).

inheritance vs composition example
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 39880
    
  28
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 39880
    
  28
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

Joined: Oct 13, 2005
Posts: 39880
    
  28
I was ignoring the possibility of mutations in genes.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

shweta sahib wrote:could you explain through code snippets.

I fail to see how this will help

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39880
    
  28
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

Joined: Jul 27, 2010
Posts: 1157

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.
 
wood burning stoves
 
subject: doubt regarding inheritance v/s composition