CSC/ECE 517 Summer 2008/wiki2 8 Inh-Del: Difference between revisions
(51 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
=Inheritance vs | =Inheritance vs Delegation= | ||
[http://en.wikipedia.org/wiki/Inheritance_(programming) Inheritance] and [http://en.wikipedia.org/wiki/Delegation_(programming) delegation] plays a very important role in object-oriented languages. Both of the techniques facilitates the reuse of code that exist in another class. This WIKI will cite examples that show cases where inheritance is better, and cases where delegation is better. I also characterize the situations in which you should employ one or the other. | |||
Inheritance and delegation plays a very important role in object-oriented languages.Both of the | |||
== | == Inheritance == | ||
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming OOP], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived or classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse existing code with little or no modification. | |||
= | The top most class is called as the parent class, base class or a superclass.The classes derived from the parent class are known as child classes or [http://www.jguru.com/faq/view.jsp?EID=27916 subclasses].The lower classes of the hierarchy inherit from the upper class. Classes get more specialized as you move toward the bottom of the hierarchy.The subclasses have more functionality than superclass because of this feature and create their own new functionality. | ||
The most common reason of using inheritance is to reuse existing classes or objects. Inheritance represent the is-a relationship between classes. For example, A mallard IS-A duck and IS-A mammal and therefore can be extended with inheritance from the base class of duck. | |||
==Example of Inheritance== | ===Example of Inheritance=== | ||
public class Car{ | public class Car{ | ||
public int gear; | public int gear; | ||
Line 40: | Line 39: | ||
} | } | ||
In this example Car is a base class where as | In this example Car is a base class where as Bmw is a derived class. Bmw has all the properties of Car (gear, speed) but it has its own functionality adjusting the height of seat. | ||
All the cars can be derived from the base class Car which has the common functionality gear and speed and each derived class can add their own fuctionality.So all the common code in the base class and specific code in the child class. | All the cars can be derived from the base class Car which has the common functionality gear and speed and each derived class can add their own fuctionality.So all the common code in the base class and specific code in the child class. | ||
== Delegation== | == Delegation== | ||
A technique,which provides the functionality of implementation of a particular interface to a field or expression is called delegation.Delegation passes a duty off to someone else. | A technique, which provides the functionality of implementation of a particular interface to a field or expression is called delegation. Delegation passes a duty off to someone else. | ||
It can be define in terms of objects ,where one object forwards certain | It can be define in terms of objects ,where one object forwards certain function calls to another object which is called its delegate. Some languages do not support direct delegation and some do not provide the feature of [http://en.wikipedia.org/wiki/Polymorphism_(computer_science)dynamic polymorphism]. | ||
The [http://http://en.wikipedia.org/wiki/Delegation_pattern delegation pattern] | Delegation is a very powerful reuse technique. The most important feature of delegation is its run time flexibility. The delegates can be changed at run time where other processes like inheritance is normally created at compile time. | ||
To be more specific [http://http://en.wikipedia.org/wiki/Delegation_pattern delegation pattern] | |||
is where an object outwardly expresses certain behavior but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility. The delegation pattern is the fundamental abstraction that underpins [http://en.wikipedia.org/wiki/Composition composition] (also referred to as aggregation), [http://en.wikipedia.org/wiki/Mixin mixins] and [http://en.wikipedia.org/wiki/Aspect aspects]. | is where an object outwardly expresses certain behavior but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility. The delegation pattern is the fundamental abstraction that underpins [http://en.wikipedia.org/wiki/Composition composition] (also referred to as aggregation), [http://en.wikipedia.org/wiki/Mixin mixins] and [http://en.wikipedia.org/wiki/Aspect aspects]. | ||
==Example of Delegation== | ===Example of Delegation=== | ||
public interface Car{ | public interface Car{ | ||
public int gear; | public int gear; | ||
Line 62: | Line 63: | ||
} | } | ||
public class Bmw | public class Bmw implements Car { | ||
public int seat_height; | public int seat_height; | ||
public void setGear(int new_gear) { | public void setGear(int new_gear) { | ||
Line 76: | Line 77: | ||
In the delegation, child class does not inherit all the properties of base class by default.All the methods that are defined in the base class should be necessarily defined in the child class and child class can add its own functionality.[http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html Inheritance Ex] | In the delegation, child class does not inherit all the properties of base class by default.All the methods that are defined in the base class should be necessarily defined in the child class and child class can add its own functionality.[http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html Inheritance Ex] | ||
==Where to use Delegation and Inheritance== | ==Where to use Delegation and Inheritance== | ||
Inheritance can not be used everywhere even if satisfies the reuse, is-a relationship and public interface.[http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf DesignPatterns] | Inheritance can not be used everywhere even if satisfies the reuse, is-a relationship and public interface.[http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf DesignPatterns] | ||
Ex:- | Ex[http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=4072925 :-] | ||
class Duck{ | class Duck{ | ||
Line 114: | Line 99: | ||
In the following example all three type of ducks inherit all the properties of a duck. But the rubber duck can neither swim nor quack. So at this point because of the inheritance property | In the following example all three type of ducks inherit all the properties of a duck. But the rubber duck can neither swim nor quack. So at this point because of the inheritance property | ||
rubber duck inherits the swim() and quack() method. It proves that inheritance | rubber duck inherits the swim() and quack() method. It proves that inheritance should not be used and this is where a method like delegation would be a better solution. RubberDuck has a color just like the mallardDuck and RedHeadDuck. so MallardDuck and RedHeadDuck are perfect to use inheritance on. | ||
Using delegation to satisfy the following case:- | Using delegation to satisfy the following case:- | ||
Line 126: | Line 111: | ||
color(); | color(); | ||
{ | { | ||
class MallardDuck implements Swim implements Quack | class MallardDuck implements Swim implements Quack ,Color{ | ||
swim(); | swim(); | ||
quack(); | quack(); | ||
color(); | color(); | ||
} | } | ||
class RedHeadDuck implements Swim implements Quack | class RedHeadDuck implements Swim implements Quack ,Color{ | ||
swim(); | swim(); | ||
quack(); | quack(); | ||
Line 140: | Line 125: | ||
} | } | ||
In | In this example there is somewhat more code but it satisfies the functionality of the different types of ducks. The mallardDuck and the RedHeadDuck has the functionality of swim, quack and color whereas, rubberDuck has only color it can neither swim nor quack. Here through delegation some additional properties of a particular duck can also be added without affection the existing code. The functionality of fly in RedHeadDuck can be added without touching the other type of ducks. | ||
interface Fly{ | interface Fly{ | ||
Line 151: | Line 136: | ||
fly(); | fly(); | ||
} | } | ||
==Listed differences between Inheritance and Delegation== | |||
* Delegation is [http://www.jguru.com/faq/view.jsp?EID=27916 dynamic] in nature whereas Inheritance is [http://en.wikipedia.org/wiki/Inheritance_(computer_science) static] in nature. Since the delegation is dynamic, the [http://en.wikipedia.org/wiki/Delegation_(programming) run] time code can be changed in delegation. Inheritance is defined at compile time. | |||
* Inheritance is faster than delegation because it is created at compile time whereas, delegation is created at run time. | |||
*Inheritance is based on a [http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=40729 is-a] relationship among classes, delegation has the property of [http://en.wikipedia.org/wiki/Has-a has-a] relation. | |||
*Delegation helps in finding bugs easily as compared to inheritance because delegation limit the scope of a possible logical error to a given subset of methods. In delegation code is more simple to explain, more easy to parse. In inheritance lots of functionality are associated with one object causes difficulty in finding bug. | |||
* Inheritance breaks [http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance encapsulation], whereas Delegation preserves encapsulation, . | |||
* By implementing delegation, all the methods laid out in the [http://www.learn-programming.za.net/programming_java_learn08.html interface] will be implemented where as in inheritance [http://en.wikipedia.org/wiki/Inheritance_(computer_science) derived] class inherit all the behavior of the base classes by default. | |||
==Conclusion== | ==Conclusion== | ||
Inheritance actually creates something from something else, where delegation is actually using something to create something else. So delegation is a | Inheritance actually creates something from something else, where delegation is actually using something to create something else. So delegation is a situation where you’re not really building new code from old code your using other code to build new. You can use inheritance where what your building IS-A of something else but if what your building has something of something else and only that part it may be better to use delegation. | ||
Delegation is gaining some popularity in code were it can be implement as a better method. Although inheritance is probably the most widely used way in Object Orientated design to use existing code as tools or patterns to create new code with out reinventing the wheel. | |||
==See Also== | |||
* '''[http://en.wikipedia.org/wiki/Delegation_(programming) Delegation]''' | * '''[http://en.wikipedia.org/wiki/Delegation_(programming) Delegation]''' | ||
* '''[http://en.wikipedia.org/wiki/Inheritance_(programming) Inheritance]''' | * '''[http://en.wikipedia.org/wiki/Inheritance_(programming) Inheritance]''' | ||
==External links== | ==External links and references== | ||
*[http://www.jguru.com/faq/view.jsp?EID=27916 Introduction] | |||
*[http://beautifulcode.oreillynet.com/2007/11/preferring_inheritance_to_dele.php Beautiful Code] | *[http://beautifulcode.oreillynet.com/2007/11/preferring_inheritance_to_dele.php Beautiful Code] | ||
*[http://groovy.codehaus.org/Replace+Inheritance+with+Delegation Replace Inheritance with Delegation] | *[http://groovy.codehaus.org/Replace+Inheritance+with+Delegation Replace Inheritance with Delegation] | ||
Line 171: | Line 168: | ||
*[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance Delegation and Composition vs. Inheritance] | *[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance Delegation and Composition vs. Inheritance] | ||
*[http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=4072925 Sun Delegation vs. Inheritance quick question] | *[http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=4072925 Sun Delegation vs. Inheritance quick question] | ||
*[http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=4072925 Inheritance vs delegation] | |||
*[http://msdn.microsoft.com/en-us/library/ms973861.aspx inheritance and interfaces] | |||
*[http://www.amazon.com/Object-Oriented-Design-Using-Java-Skrien/dp/0072974168 Object-Oriented Design Using Java, Dale Skrien, 2007] | |||
[http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Summer_2008/wiki2_Assignment Back to assignment page] |
Latest revision as of 01:35, 9 July 2008
Inheritance vs Delegation
Inheritance and delegation plays a very important role in object-oriented languages. Both of the techniques facilitates the reuse of code that exist in another class. This WIKI will cite examples that show cases where inheritance is better, and cases where delegation is better. I also characterize the situations in which you should employ one or the other.
Inheritance
In object-oriented programming OOP, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived or classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse existing code with little or no modification.
The top most class is called as the parent class, base class or a superclass.The classes derived from the parent class are known as child classes or subclasses.The lower classes of the hierarchy inherit from the upper class. Classes get more specialized as you move toward the bottom of the hierarchy.The subclasses have more functionality than superclass because of this feature and create their own new functionality.
The most common reason of using inheritance is to reuse existing classes or objects. Inheritance represent the is-a relationship between classes. For example, A mallard IS-A duck and IS-A mammal and therefore can be extended with inheritance from the base class of duck.
Example of Inheritance
public class Car{ public int gear; public int speed; public Car(int speed_of_car, int gear_of_car) { gear = speed_of_car; speed = gear_of_car; } public void setGear(int new_gear) { gear = new_gear; } public void speedUp(int increment_of_speed) { speed += increment_of_speed; } } public class Bmw extends Car { public int seat_height; public Toyota(int speed_of_car, int new_gear , int new_seat_height) { super( speed_of_car, new_gear); seat_height = new_seat_height; } public void set_height(int height) { seat_height= height; } }
In this example Car is a base class where as Bmw is a derived class. Bmw has all the properties of Car (gear, speed) but it has its own functionality adjusting the height of seat. All the cars can be derived from the base class Car which has the common functionality gear and speed and each derived class can add their own fuctionality.So all the common code in the base class and specific code in the child class.
Delegation
A technique, which provides the functionality of implementation of a particular interface to a field or expression is called delegation. Delegation passes a duty off to someone else. It can be define in terms of objects ,where one object forwards certain function calls to another object which is called its delegate. Some languages do not support direct delegation and some do not provide the feature of polymorphism.
Delegation is a very powerful reuse technique. The most important feature of delegation is its run time flexibility. The delegates can be changed at run time where other processes like inheritance is normally created at compile time.
To be more specific delegation pattern is where an object outwardly expresses certain behavior but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility. The delegation pattern is the fundamental abstraction that underpins composition (also referred to as aggregation), mixins and aspects.
Example of Delegation
public interface Car{ public int gear; public int speed; public void setGear(int new_gear) { gear = new_gear; } public void speedUp(int increment_of_speed) { speed += increment_of_speed; } } public class Bmw implements Car { public int seat_height; public void setGear(int new_gear) { gear = new_gear; } public void speedUp(int increment_of_speed) { speed += increment_of_speed; } public void set_height(int height) { seat_height= height; } }
In the delegation, child class does not inherit all the properties of base class by default.All the methods that are defined in the base class should be necessarily defined in the child class and child class can add its own functionality.Inheritance Ex
Where to use Delegation and Inheritance
Inheritance can not be used everywhere even if satisfies the reuse, is-a relationship and public interface.DesignPatterns Ex:-
class Duck{ swim(); quack(); color(); } class MallradDuck extends Duck{ display(); } class RubberDuck extends Duck{ display(); } class RedHeadDuck extends Duck{ display(); }
In the following example all three type of ducks inherit all the properties of a duck. But the rubber duck can neither swim nor quack. So at this point because of the inheritance property rubber duck inherits the swim() and quack() method. It proves that inheritance should not be used and this is where a method like delegation would be a better solution. RubberDuck has a color just like the mallardDuck and RedHeadDuck. so MallardDuck and RedHeadDuck are perfect to use inheritance on.
Using delegation to satisfy the following case:-
interface Swim{ swim(); { interface Quack{ quack(); { interface Color{ color(); { class MallardDuck implements Swim implements Quack ,Color{ swim(); quack(); color(); } class RedHeadDuck implements Swim implements Quack ,Color{ swim(); quack(); color(); } class RubberDuck implements Color{ color(); }
In this example there is somewhat more code but it satisfies the functionality of the different types of ducks. The mallardDuck and the RedHeadDuck has the functionality of swim, quack and color whereas, rubberDuck has only color it can neither swim nor quack. Here through delegation some additional properties of a particular duck can also be added without affection the existing code. The functionality of fly in RedHeadDuck can be added without touching the other type of ducks.
interface Fly{ fly(); { class RedHeadDuck implements Swim implements Quack implements Color implements Fly{ swim(); quack(); color(); fly(); }
Listed differences between Inheritance and Delegation
- Delegation is dynamic in nature whereas Inheritance is static in nature. Since the delegation is dynamic, the run time code can be changed in delegation. Inheritance is defined at compile time.
- Inheritance is faster than delegation because it is created at compile time whereas, delegation is created at run time.
- Inheritance is based on a is-a relationship among classes, delegation has the property of has-a relation.
- Delegation helps in finding bugs easily as compared to inheritance because delegation limit the scope of a possible logical error to a given subset of methods. In delegation code is more simple to explain, more easy to parse. In inheritance lots of functionality are associated with one object causes difficulty in finding bug.
- Inheritance breaks encapsulation, whereas Delegation preserves encapsulation, .
- By implementing delegation, all the methods laid out in the interface will be implemented where as in inheritance derived class inherit all the behavior of the base classes by default.
Conclusion
Inheritance actually creates something from something else, where delegation is actually using something to create something else. So delegation is a situation where you’re not really building new code from old code your using other code to build new. You can use inheritance where what your building IS-A of something else but if what your building has something of something else and only that part it may be better to use delegation.
Delegation is gaining some popularity in code were it can be implement as a better method. Although inheritance is probably the most widely used way in Object Orientated design to use existing code as tools or patterns to create new code with out reinventing the wheel.
See Also
External links and references
- Introduction
- Beautiful Code
- Replace Inheritance with Delegation
- Inheritance vs Delegation: pros and cons
- Avoid Java Delegation
- Delegation and Composition vs. Inheritance
- Sun Delegation vs. Inheritance quick question
- Inheritance vs delegation
- inheritance and interfaces
- Object-Oriented Design Using Java, Dale Skrien, 2007