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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 55: Line 55:
  public class Bmw implementation Car {
  public class Bmw implementation Car {
     public int seat_height;
     public int seat_height;
     public void setGear(int new_gear) {
     public void setGear(int new_gear) {
         gear = new_gear;
         gear = new_gear;

Revision as of 12:31, 22 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.

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.

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

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.

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.