File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Composition and Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Composition and Inheritance" Watch "Composition and Inheritance" New topic
Author

Composition and Inheritance

Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
What is the difference between Composition and Inheritance in java.
Thanks.


Thanks,
Mathews
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

In Java, or in any language, inheritance is, well, inheritance -- as expressed by the "extends" keyword; while composition refers to the inclusion of one object inside another as a member.
In this example, Bar gets access to Foo's capabilites via inheritance:

While here it gets them via composition:

The general, oversimplified rule is to use composition when you can, inheritance when you must.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Composition and Inheritance
 
Similar Threads
Inheritance and composition
A refactoring question.
Composition versus Inheritance
Inheritance or composition?