All Topics  
Inheritance (computer science)

 

   Email Print
   Bookmark   Link






 

Inheritance (computer science)



 
 
In object-oriented programming
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
, inheritance is a way to form new class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
es (instances of which are called object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
s) using classes that have already been defined. The inheritance concept was invented in 1967 for Simula
Simula

Simula is a name for two programming languages, Simula I and Simula 67, developed in the 1960s at the Norwegian Computing Center in Oslo, by Ole-Johan Dahl and Kristen Nygaard....
.

The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse
Reusability

In computer science and software engineering, reusability is the likelihood a segment of source code can be used again to add new functionalities with slight or no modification....
 existing code with little or no modification.

Inheritance provides the support for representation by categorization
Categorization

Categorization is the process in which ideas and objects are recognition, difference and understanding. Categorization implies that objects are grouped into categories, usually for some specific purpose....
  in computer languages.






Discussion
Ask a question about 'Inheritance (computer science)'
Start a new discussion about 'Inheritance (computer science)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In object-oriented programming
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
, inheritance is a way to form new class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
es (instances of which are called object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
s) using classes that have already been defined. The inheritance concept was invented in 1967 for Simula
Simula

Simula is a name for two programming languages, Simula I and Simula 67, developed in the 1960s at the Norwegian Computing Center in Oslo, by Ole-Johan Dahl and Kristen Nygaard....
.

The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse
Reusability

In computer science and software engineering, reusability is the likelihood a segment of source code can be used again to add new functionalities with slight or no modification....
 existing code with little or no modification.

Inheritance provides the support for representation by categorization
Categorization

Categorization is the process in which ideas and objects are recognition, difference and understanding. Categorization implies that objects are grouped into categories, usually for some specific purpose....
  in computer languages. Categorization is a powerful mechanism number of information processing, crucial to human learning by means of generalization (what is known about specific entities is applied to a wider group given a belongs relation can be established) and cognitive economy (less information needs to be stored about each specific entity, only its particularities).

Inheritance is also sometimes called generalization, because the is-a
Is-a

In knowledge representation and object-oriented programming and Object-oriented design , is-a is a relationship where one class D is a subclass of another class B ....
 relationships represent a hierarchy between classes of objects. For instance, a "fruit" is a generalization of "apple", "orange", "mango" and many others. One can consider fruit to be an abstraction of apple, orange, etc. Conversely, since apples are fruit (i.e., an apple is-a fruit), apples may naturally inherit all the properties common to all fruit, such as being a fleshy container for the seed of a plant.

An advantage of inheritance is that modules with sufficiently similar interfaces can share a lot of code, reducing the complexity of the program. Inheritance therefore has another view, a dual, called polymorphism, which describes many pieces of code being controlled by shared control code.

Inheritance is typically accomplished either by overriding
Method overriding (programming)

Method overriding, in object oriented programming, is a language feature that allows a Subclass to provide a specific implementation of a method that is already provided by one of its superclass es....
 (replacing) one or more methods exposed by ancestor, or by adding new methods to those exposed by an ancestor.

Complex inheritance, or inheritance used within a design that is not sufficiently mature, may lead to the Yo-yo problem
Yo-yo problem

In computer science, the yo-yo problem is an anti-pattern that occurs when a programmer has to read and understand a program whose inheritance graph is so long and complicated that the programmer has to keep flipping between many different class definitions in order to follow the control flow of the program....
.

Applications of inheritance

There are many different aspects to inheritance. Different uses focus on different properties, such as the external behavior of objects, internal structure of the object, structure of the inheritance hierarchy, or software engineering properties of inheritance. Sometimes it's desirable to distinguish these uses, as it's not necessarily obvious from context.

Specialization

One common reason to use inheritance is to create specializations of existing classes or objects. This is often called subtyping when applied to classes. In specialization, the new class or object has data or behavior aspects that are not part of the inherited class. For example, a "Bank Account" class might have data for an "account number", "owner", and "balance". An "Interest Bearing Account" class might inherit "Bank Account" and then add data for "interest rate" and "interest accrued" along with behavior for calculating interest earned.

Another form of specialization occurs when a base class specifies that it has a particular behavior but does not actually implement the behavior. Each non-abstract, concrete class which inherits from that abstract class must provide an implementation of that behavior. This providing of actual behavior by a subclass
Subclass (computer science)

In object-oriented programming, a subclass is a class that Inheritance some properties from its superclass .You can usually think of the subclass as being "a kind of" its superclass, as in a "a Manx is a kind of cat", or "a square is a kind of rectangle":...
 is sometimes known as implementation
Implementation inheritance

In programming, Implementation inheritance is the inheritance of the full functionality of a Class , as opposed to the inheritance of an interface , which simply defines the methods that must be present....
 or reification.

Overriding

Many object-oriented programming languages permit a class or object to replace the implementation of an aspect—typically a behavior—that it has inherited. This process is usually called overriding. Overriding introduces a complication: which version of the behavior does an instance of the inherited class use—the one that is part of its own class, or the one from the parent (base) class? The answer varies between programming languages, and some languages provide the ability to indicate that a particular behavior is not to be overridden and behave.

Code re-use

One of the earliest motivations for using inheritance was to allow a new class to re-use code which already existed in another class. This practice is usually called implementation inheritance.

In most quarters, class inheritance for the sole purpose of code re-use has fallen out of favor. The primary concern is that implementation inheritance does not provide any assurance of polymorphic
Polymorphism in object-oriented programming

In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B....
 substitutability—an instance of the re-using class cannot necessarily be substituted for an instance of the inherited class. An alternative technique, delegation
Delegation pattern

In software engineering, the delegation pattern is a technique where an object outwardly expresses certain behaviour but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility....
, requires more programming effort but avoids the substitutability issue. In C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 private inheritance can be used as form of implementation inheritance without substitutability. Whereas public inheritance represents an "is-a
Is-a

In knowledge representation and object-oriented programming and Object-oriented design , is-a is a relationship where one class D is a subclass of another class B ....
" relationship and delegation represents a "has-a
Has-a

In database design and object oriented program architecture, has-a is a relationship where one object "belongs" to another object , and behaves according to the rules of ownership....
" relationship, private (and protected) inheritance can be thought of as an "is implemented in terms of" relationship.

Object Oriented-Software Construction, 2nd edition
Object-Oriented Software Construction

Object-Oriented Software Construction is the title of a book by Bertrand Meyer, widely considered a foundational text of object-oriented programming....
 by Bertrand Meyer
Bertrand Meyer

Bertrand Meyer is an academic, author, and consultant in the field of computer languages. He created the Eiffel ....
, the creator of the object-oriented programming language Eiffel
Eiffel (programming language)

Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
, lists twelve different uses of inheritance , most of which involve some amount of implementation inheritance.

Limitations and alternatives

When using inheritance extensively in designing a program, one should be aware of certain constraints that it imposes.

For example, consider a class Person that contains a person's name, address, phone number, age, gender, and race. We can define a subclass of Person called Student that contains the person's grade point average and classes taken, and another subclass of Person called Employee that contains the person's job title, employer, and salary.

In defining this inheritance hierarchy we have already defined certain restrictions, not all of which are desirable:

Constraints of inheritance-based design

  • Singleness: using single inheritance, a subclass can inherit from only one superclass. Continuing the example given above, Person can be either a Student or an Employee, but not both. Using multiple inheritance
    Multiple inheritance

    Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
     partially solves this problem, as a StudentEmployee class can be defined that inherits from both Student and Employee. However, it can still inherit from each superclass only once; this scheme does not support cases in which a student has two jobs or attends two institutions.
  • Static: the inheritance hierarchy of an object is fixed at instantiation when the object's type is selected and does not change with time. For example, the inheritance graph does not allow a Student object to become a Employee object while retaining the state of its Person superclass. (Although similar behavior can be achieved with the decorator pattern
    Decorator pattern

    In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing Class dynamically....
    .)
  • Visibility: whenever client code has access to an object, it generally has access to all the object's superclass data. Even if the superclass has not been declared public, the client can still cast the object to its superclass type. For example, there is no way to give a function a pointer to a Student's grade point average and transcript without also giving that function access to all of the personal data stored in the student's Person superclass.


Roles and inheritance

Sometimes inheritance based design is used instead of roles. A role, say Student role of a Person describes a characteristic associated to the object that is present because the object happens to participate in some relationship with another object (say the person in student role -has enrolled- to the classes). Some object-oriented design methods do not distinguish this use of roles from more stable aspects of objects. Thus there is a tendency to use inheritance to model roles, say you would have a Student role of a Person modelled as a subclass of a Person. However, neither the inheritance hierarchy nor the types of the objects can change with time. Therefore, modelling roles as subclasses can cause the roles to be fixed on creation, say a Person cannot then easily change his role from Student to Employee when the circumstances change. From modelling point of view, such restrictions are often not desirable, because this causes artificial restrictions on future extensibility of the object system, which will make future changes harder to implement, because existing design needs to be updated. Inheritance is often better used with a generalization mindset, such that common aspects of instantiable classes are factored to superclasses; say having a common superclass 'LegalEntity' for both Person and Company classes for all the common aspects of both. The distinction between role based design and inheritance based design can be made based on the stability of the aspect. Role based design should be used when it's conceivable that the same object participates in different roles at different times, and inheritance based design should be used when the common aspects of multiple classes (not objects!) are factored as superclasses, and do not change with time.

One consequence of separation of roles and superclasses is that compile-time and run-time aspects of the object system are cleanly separated. Inheritance is then clearly a compile-time construct. Inheritance does influence the structure of many objects at run-time, but the different kinds of structure that can be used are already fixed at compile-time.

To model the example of Person as an employee with this method, the modelling ensures that a Person class can only contain operations or data that are common to every Person instance regardless of where they are used. This would prevent use of a Job member in a Person class, because every person does not have a job, or at least it is not known that the Person class is only used to model Person instances that have a job. Instead, object-oriented design would consider some subset of all person objects to be in an "employee" role. The job information would be associated only to objects that have the employee role. Object-oriented design would also model the "job" as a role, since a job can be restricted in time, and therefore is not a stable basis for modelling a class. The corresponding stable concept is either "WorkPlace" or just "Work" depending on which concept is meant. Thus, from object-oriented design point of view, there would be a "Person" class and a "WorkPlace" class, which are related by a many-to-many associatation "works-in", such that an instance of a Person is in employee role, when he works-in a job, where a job is a role of his work place in the situation when the employee works in it.

Note that in this approach, all classes that are produced by this design process are part of the same domain, that is, they describe things clearly using just one terminology. This is often not true for other approaches.

The difference between roles and classes is especially difficult to understand if referential transparency is assumed, because roles are types of references and classes are types of the referred-to objects.

Types of Inheritance

A new class can be derived from an existing class. Then the new derived class is called derived class (or sub class) and the existing class is called base class (or super class).

I. Single Inheritance: When a derived class inherits only form one base class, it is known as single inheritance.

II. Multiple Inheritance: When a sub class inherits from multiple base class, it is known as multiple inheritance.for Example Base class X Y and derived class is Z ( which is derived from both class X and Y) III. Multilevel Inheritance: When a sub class inherits form a class that itself inherits from another class, it is known as multilevel inheritance. For Example; Class X ------ Base class of Y Class Y ------ Sub class of X and Base class of Z Class Z ------ Sub class of Y

IV. Hierarchical Inheritance: when many sub classes inherit form a single base class, it is known as hierarchical inheritance. For Example: Class W ------ Base class Class X ------ Sub class or Derived class (from class W) Class Y ------ Sub class or Derived class (from class W) Class Z ------ Sub class or Derived class (from class W) V. Hybrid Inheritance: When a subclass inherit from multiple base class and all of its base class inherit from a single base class, this form of inheritance is known as hybrid inheritance. For Example: Class W ------ Base class Class X & Class Y ------ Derived class (From Class W) Class Z ------ Derived class ( from class X and class Y) NOTE: It is not necessary that the class are always named as W, X, Y, Z

by Gaurav.reader (talk) 13:20, 26 February 2009 (UTC)

See also

  • Circle-ellipse problem
    Circle-ellipse problem

    The circle-ellipse problem in software development illustrates a number of pitfalls which can arise when using subtype polymorphism in object modelling....
  • Class (object-oriented programming)
  • Composition in object-oriented programming
  • Hierarchy (object-oriented programming)
    Hierarchy (object-oriented programming)

    In computer science's object-oriented programming, the mapped relationships of class is known as a hierarchy. This can be visualized as an upside-down tree , the top of which is known as the root ....
  • Implementation inheritance
    Implementation inheritance

    In programming, Implementation inheritance is the inheritance of the full functionality of a Class , as opposed to the inheritance of an interface , which simply defines the methods that must be present....
  • Inheritance semantics
    Inheritance semantics

    Inheritance semantics is an important concept in object-oriented programming....
  • Interface (computer science)
    Interface (computer science)

    Interface generally refers to an Abstraction_%28computer_science%29 that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide Polymorphism in object-orien...
  • Polymorphism in object-oriented programming
    Polymorphism in object-oriented programming

    In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B....
  • Multiple inheritance
    Multiple inheritance

    Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
  • Override (object-oriented programming)
  • Virtual inheritance
    Virtual inheritance

    In the C++ programming language, virtual inheritance is a kind of Inheritance that solves some of the problems caused by multiple inheritance by clarifying ambiguity over which ancestor class members to use....
  • Differential inheritance
    Differential inheritance

    Differential Inheritance is a common inheritance model used by Prototype-based programming programming languages such as JavaScript, Io programming language and NewtonScript....
  • The Third Manifesto
    The Third Manifesto

    The Third Manifesto is Christopher J. Date's and Hugh Darwen's proposal for future relational database management systems that would avoid 'object-relational impedance mismatch' between object-oriented programming languages and RDBMSs by fully supporting all the capabilities of the relational model....
  • Role-Oriented Programming
    Role-Oriented Programming

    Role-oriented programming is a form of programming language aimed at expressing things in terms which are analogous to our conceptual understanding of the world....