Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Composition Vs Inheritance

 
prasad bhn
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please explain me what is the difference between composition and inheritance.
I would appreciate the help from anybody.
Thanks in advance,
Prasad Bh.
 
Laojar Chuger
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, composition implements the relationship of "has a" and inhertance implements the relationship "is a".
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Composition means that a class is composed of implementations of interfaces instead of inheriting those behaviors from some other class.
 
David Roberts
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of composition is the attribute reference, ie.
composition:
public class A
{
AnotherClass B = new AnotherClass();
}
inheritance:
public class A extends B
{
}
or
public class A implements B
{
}

------------------
David Roberts - SCJP2,MCP
 
prasad bhn
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies friends.
Best Regards,
prasad bhn.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic