CSC/ECE 517 Fall 2010/ch3 3b sv

From Expertiza_Wiki
Jump to navigation Jump to search

Multiple Inheritance and Mixins

Inheritance is a concept of Object Oriented Programming (OOP) that facilitates reuse and compartmentalization of code by creating objects ( which are collections of attributes and behaviors ) which can be derived from existing objects. The deriving object(class) is termed as the child class/sub-class/derived class , and, the object(class) whose methods are being used is called the parent class/super-class/base class. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as Single Inheritance if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.

Definitions

Multiple Inheritance

If a child class inherits behaviors and attributes from two or more parent classes, it is termed as Multiple Inheritance. Languages supporting this feature include C++,Perl and Python. The main advantage of Multiple Inheritance is that it enables an user to combine independent(and not so independent) concepts(classes) into a composite concept called the derived class. For instance,given a set of interaction base classes and a set of base classes defining display options, the user can create a new window by selecting a style of window interaction and appearance from the given base classes.

Suppose there are N concepts, each of which can be combined with one of M other concepts, N+M classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N>=2 and M>=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication.

Interfaces

Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language simple,object oriented and familiar. Interfaces were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword interface) are abstract type containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates.

Modules

A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity. A module "protects" the names it contains. The primary benefits of using modules are:

  • The code is more organized.
  • The risk of name clashes is eliminated as modules provide namespace facility.
  • Mixins can be implemented using modules.

Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specifies to be added to a particular class or object.

Mixin

Mixins are one of the defining features of Ruby that is closely related to Duck typing. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods.

Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins.

Traits

A trait can be considered as a collection of methods which serves as a simple model for structured object oriented programs. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Traits is not an abstract type and implements methods.

With Traits, classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript.

Diamond Problem : Loophole in Multiple Inheritance

With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.

But multiple inheritance has the following two problems:

  • It is possible that a class inherits multiple features with the same name.
  • There can be more than one path to a given ancestor from a class ( known as the diamond problem or fork-join inheritance )

The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The diamond problem is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.

Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular typechecking of multiple dispatch.