Log in / create account | Login with OpenID
DocForge
An Open Wiki For Software Developers

Inheritance

From DocForge

Inheritance is an object oriented programming concept of objects being derived and partly defined from others, sharing behaviors (methods / functions) and characteristics (properties / fields). An object or type is said to inherit or extend another object or type.

Contents

Polymorphism [edit]

One of the most powerful object-oriented tools is known as polymorphism. Taking apart the word, we get poly and morph. This roughly means that something can be treated as many different things. Thus, polymorphism is the ability of objects to treat themselves as instances of their base classes. Thus, if I have a class canine, and a class beagle which extends canine, anything I can do to objects of class canine, I can do to beagle. Likewise, I can have a function receive a object of type canine, and that function can then receive beagle, or perhaps another class chihuahua which also extends canine.

This powerful tool is implemented in different ways by different programming languages.

Multiple Inheritance [edit]

Languages such as C++ support multiple inheritance, where a type can inherit from more than one parent type. This facilitates the implementation of something otherwise known as an interface, which is a kind of guarantee that an object will implement a certain set of instructions. Multiple inheritance, however, also allows for an object to inherit fields (data) from its base classes, as well as default function behaviors.

Single Inheritance [edit]

Languages such as Java take a single-inheritance approach, where any class can only have a single base class. To support polymorphism, single-inheritance schemes usually have a tool similar to the Java interface, which is like a class, only it does not allow for the implementation of any behaviors or fields. It only allows for the definition of functions. While this is severely limiting in many situations, it often forces the programmer to organize his or her object hierarchy in a much more rigid, structured way, which can be advantageous for long-term use. However, C++ speed fans will probably prefer the increased freedom of multiple-inheritance. The choice is entirely up to the programmer, and a competent programmer should have no trouble adapting to either scheme.

See Also [edit]