CSC/ECE 517 Summer 2008/wiki2 8 Inh-Del: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 111: Line 111:
*[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]
*[http://www.perlmonks.org/index.pl?node_id=278375]
*[http://www.perlmonks.org/index.pl?node_id=278375 Inheritance vs Delegation: pros and cons]
*[http://articles.techrepublic.com.com/5100-10878_11-5031837.html Avoid Java Delegation]
*[http://articles.techrepublic.com.com/5100-10878_11-5031837.html Avoid Java Delegation]
*[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance]
*[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance]
*[http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=4072925 Sun]
*[http://forum.java.sun.com/thread.jspa?threadID=702260&messageID=4072925 Sun]

Revision as of 01:00, 24 June 2008

Inheritance vs Delagation

Inheritance - delegation

Inheritance and delegation plays a very important role in object-oriented languages.Both of the technique facilitates the reuse of code that exist in another class.

Problem Definition

Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.

Inheritance

Hierarchical code structure, which keep most of the common code at the top most hierarchy ,is called inheritance.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 code can be reused up to lower levels of the hierarchy. Classes get more specialized as you move toward the bottom of the hierarchy.The subclasses inherits all the functionality of the superclass class.The subclasses have more functionality than superclass because they inherit the functionality of the superclass class and have thier own functionality.The one of the most common reason of using inheritance is to reuse the existing classes or objects. Inheritance represent the is-a relationship between classes.Inheritance is static by nature and determined at compile time.

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 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.

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 Bmv is a derived class.Bmv 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 fuction calls to another object, called its delegate.All languages does not suppport direct delegation and it does not provide the feature of polymorphism. Delegation is a very powerful reuse technique.The most important feature of delegation is ,run time flexibility.The delegates can be changed at run time .

The 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 implementation 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

Differences between Inheritance and Delegation

  • Delegation is dynamic dynamic in nature whereas Inheritance is static in nature. Since the delegaion is dynamic, the run time code can be changed in delegation and inheritance is defined at compile time.
  • A derived class can be inherited only from one base class i.e multiple inheritance is not allowed.In delegation derived classes can be implemented with more one than one class.
  • Inheritance is faster than delegation because it is created at compile time where delegation is created at run time.
  • Inheritance is based on a Is-a relationship among classes, delegation has the property of has-a relation.Is-a
  • Interfaces helps in finding bugs easily as compared to inheritance because interface limit the scope of a possible logic error to a given subset of methods.
  • Interface code is more simple to explain, more easy to parse,and it does not break existing code. It is considered one of the major roles for doing OO design.
  • Inheritance breaks encapsulation, whereas Delegation preserves encapsulation.encapsulation
  • By implementing the interface we guarantee that 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.
  • In languages like Ruby and Java are only able to do single inheritance. This limits the capability of using the same function to do multiple functions with the same command. This is not a problem with delegation.

Conclusion

Inheritance actually creates something from something else, where delegation is actually using something to create something else. So delegation is a situation where your not really building new code from old code your using other code to build new.

Inheritance has issues with being able to do dual inheritance in some languages where Delegation may be a better solution in those environments. However Inheritance has a bigger following by programmers. Its a simple easy way to make something from something else and allows the programmer to use tools that were created by other. Trying to troubleshoot or understand code when the inherited code is calling code that you didn't write or understand may be very difficult. Delegation is not really a programming language concept, but it is a concept that has been defined as a virtual methods in inheritance.

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 Orentiated design to use exsinting code as tools or patterns to create new code with out reinventing the wheel.

See Also

External links