CSC/ECE 517 Fall 2011/ch4 4c ap: Difference between revisions
Line 23: | Line 23: | ||
==Advantages of Multiple Inheritance== | ==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 suclasses 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== | ==Disadvantages of Multiple Inheritance== |
Revision as of 17:52, 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 suclasses encapsulate the implementation details of the interface.
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.
Another example