Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2007, 02:20 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Delegation vs inheritance
Hi everyone,

I am interested in knowing the difference between inheritance and delegation pattern. When to use what?

Regards.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-07-2007, 10:07 PM
Member
 
Join Date: Nov 2007
Posts: 7
Programmist is on a distinguished road
If you're interested in design patterns you can go here, or do a Google search.

I don't really consider inheritance and delegation to be major design patterns though. They are more like basic OO concepts.

Inheritance is used to create a hierarchical-type code structure that tries to keep as much "common" code near the top of the hierarchy, so it can be reused by lower levels of the hierarchy. So, in this approach, classes get more specialized as you move toward the bottom of the hierarchy. In small, static systems, inheritance can be ok. But large inheritance chains can also lead to hard-to-maintain code. Read up on design patterns that favor composition over inheritance for more info when to use inheritance and when not to.

Delegation is simply passing a duty off to someone/something else. Here is a simple example:

Code:
public class Information { public String getTemperature() { return TempGetter.getTemperature(); } public String getTime() { return TimeGetter.getTime(); } }
In this example, I've got imaginary classes TimeGetter and TempGetter that have static methods for retrieving time and temperature. Rather than finding my own way to obtain these data, I use existing classes. Said another way, I delegate these tasks to these classes without knowing/caring about the details of how it gets done.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply

« Java rant | Graphs »

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritance mew New To Java 1 12-07-2007 07:08 PM
Using interfaces with Delegation Java Tip Java Tips 0 12-06-2007 02:49 PM
Using Delegation in Java Java Tip Java Tips 0 12-06-2007 02:44 PM
Multiple Inheritance mew New To Java 1 12-01-2007 11:04 PM
Inheritance in GUI Marty SWT / JFace 2 05-11-2007 01:54 AM


All times are GMT +3. The time now is 07:08 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org