Inheritance in Programming Languages

Inheritance in Programming Languages

Krishnaprasad Thirunarayan
DOI: 10.4018/978-1-60566-026-4.ch321
OnDemand:
(Individual Chapters)
Available
$37.50
No Current Special Offers
TOTAL SAVINGS: $37.50

Abstract

Inheritance is a powerful concept employed in computer science, especially in artificial intelligence (AI), object-oriented programming (OOP), and object-oriented databases (OODB). In the field of AI, inheritance has been primarily used as a concise and effective means of representing and reasoning with common-sense knowledge (Thirunarayan, 1995). In programming languages and databases, inheritance has been used for the purpose of sharing data and methods, and for enabling modularity of software (re)use and maintenance (Lakshmanan & Thirunarayan, 1998). In this chapter, we present various design choices for incorporating inheritance into programming languages from an application programmer’s perspective. In contrast with the language of mathematics, which is mature and well-understood, the embodiment of object-oriented concepts and constructs in a concrete programming language is neither fixed nor universally accepted. We exhibit programs with similar syntax in different languages that have very different semantics, and different looking programs that are equivalent. We compare and contrast method inheritance, interaction of type system with method binding, constructs for method redefinition, and their implementation in widely used languages such as C++ (Stroustrup, 1997), Java (Arnold, Gosling, & Holmes, 2005), and C# (Hejlsberg, Wiltamuth, & Golde, 2006), to illustrate subtle issues of interest to programmers. Finally, we discuss multiple inheritance briefly.
Chapter Preview
Top

Background

SIMULA introduced the concepts of object, class, inheritance, and polymorphism for describing discrete event simulations (Meyer, 1999). Subsequently, object-oriented programming languages such as Smalltalk, C++, Object Pascal., and so forth used these concepts for general purpose programming (Budd, 2002).

Object/Instance is a run-time structure with state and behavior. Object state is stored in its fields (variables) and behavior as its methods (functions). Class is a static description of objects. (In practice, a class itself can appear as a run-time structure manipulated using reflection APIs such as in Java, C#, CLOS, and so forth.) A class defines type of each field and code for each method, which inspects and/or transforms field values. (By field we mean instance field, and by method we mean instance method. The discussion of static fields and static methods, and access control primitives such as private, protected, and private, are beyond the scope of this chapter.) Inheritance is a binary relation between classes (say P and Q) that enables one to define a class (Q) in terms of another class (P) incrementally, by adding new fields, adding new methods, or modifying existing methods through overriding. A class Q is a subclass of class P if class Q inherits from class P.

class P { 
int i;
int f() { return 2;}
int f1() { return f() + i;}
void g() {}
}
class Q extends P { 
int j;
int f() { return 4;}
int h() { return i + j;}
}
class Main {
public static void main(String [] args) {
Q q = new Q();
P p = q;
p.f1();
}

}Every P-object has an int field i, and methods f(), f1() and g() defined on it. Every Q-object has int fields i and j, and methods f(), f1(), g() and h() defined on it. Q inherits i, f1(), and g() from P, and overrides f() from P.

Key Terms in this Chapter

Class: A static description of objects. It defines types for fields and code for methods.

Subclass (Inheritance): Inheritance is a binary relation between classes that enables one to define a class in terms of another class incrementally, by adding new fields, adding new methods, or modifying existing methods through overriding. A class Q is a subclass of class P if class Q inherits from class P.

Subtype (Polymorphism): A class Q is a subtype of a class P if an instance of Q can be used where an instance of P is required. A variable is said to be polymorphic if it can hold a reference to objects of different forms. Typically, a variable of type P can hold a reference to an instance of a subclass of class P.

Static/Dynamic Binding: Binding is the association of method code to a method call. Binding carried out at compile-time is called static binding, while the binding carried out at run-time is called dynamic binding.

Strong/Static/Dynamic Typing: A strongly typed language guarantees that in a program all the operations are applied to operands of compatible type, and any type violation is flagged by the language implementation. A statically typed language makes such guarantees by compile-time analysis of a program. A dynamically typed language makes such guarantees using run-time checks. Modern object-oriented languages such as Java and C# are strongly typed and straddle the two extremes, by checking type constraints statically as much as possible, and generating code for performing additional type constraints at run-time in situations where those checks are data dependent.

Object: A run-time structure with state and behavior. Object state is stored in its fields (variables) and behavior as its methods (functions).

Complete Chapter List

Search this Book:
Reset