CSC/ECE 517 Fall 2011/ch4 4c ap: Difference between revisions
No edit summary |
|||
Line 55: | Line 55: | ||
==Disadvantages of Multiple Inheritance== | ==Disadvantages of Multiple Inheritance== | ||
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows: | |||
'''1. Name collision''' | |||
Two features (instance variables or methods) with the same name are inherited from different super-classes. | |||
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()''). | |||
In Java, some people call this situation the "Deadly Diamond of Death". This is illustrated by the figure below: | |||
figure | |||
'''2. Repeated inheritance''' | |||
'''3. Method combination''' | |||
'''4. Implementation difficulties''' | |||
'''5. Misuse''' | |||
==Extending Specific Objects== | ==Extending Specific Objects== |
Revision as of 18:12, 16 October 2011
Modules and Mixins
Regular Expressions
Modules and Mixins
Mixins
Comparable
Composing Modules
Simulating Multiple Inheritance
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Mutiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.
Example - Taggable string
Advantages of Multiple Inheritance
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:
1. Multiple Independant Protocols
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes.
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. Another example
2. Mix and Match
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class.
For example, Mixins help achieve this. Another example
3. Submodularity
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. Another example
4. Separation of interface and implementation
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation. Another example
Disadvantages of Multiple Inheritance
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:
1. Name collision
Two features (instance variables or methods) with the same name are inherited from different super-classes.
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, initialize()).
In Java, some people call this situation the "Deadly Diamond of Death". This is illustrated by the figure below:
figure
2. Repeated inheritance
3. Method combination
4. Implementation difficulties
5. Misuse