Object-oriented methodology is a way of viewing software components and their relationships. Object-oriented methodology relies on three characteristics that define object-oriented languages: encapsulation, polymorphism, and inheritance. These three terms are elaborated below.
An object is an encapsulation of data together with procedures that manipulate the data and functions that return information about the data. The procedures and functions are both called methods.
Encapsulation refers to mechanisms that allow each object to have its own data and methods. The idea of encapsulating data together with methods existed before object-oriented languages were developed. It is, for example, inherent in the concept of an abstract data type. Objects can be implemented in classical languages such as C using separate compilation or structs to provide the encapsulation. This is a common technique for implementing abstract data types. See Implementing Objects in C for a brief explanation and example.
Object-oriented languages provide more powerful and flexible encapsulation mechanisms for restricting interactions between components. When used carefully, these mechanisms allow the software developers to restrict the interactions between components to those that are required to achieve the desired functionality. The management of component interactions is an important part of software design. It has a significant impact on the ease of understanding, testing, and maintenance of components.
In object-oriented languages, this encapsulation is effected in part by having all method calls handled by objects that recognize the method. This leads to a different syntax for calling methods. For example, in C, you might have a table data structure with a procedure called add for adding a new entry. The declaration for this procedure could be
void add(Table t, Data dat, Key k);A typical call for this function is
add(t, dat, k);
In the object-oriented language Java, a method with similar effect could be declared by
void add(Data dat, Key k);and a typical call would be
t.add(dat, k);
In this call, t plays a special role: it recognizes and carries out the add method. In object-oriented terminology, this method call is referred to as a message, and t is the receiver of the message. One benefit of this approach is that there can be many methods named "add", with different objects implementing them in different ways. This allows programmers to reuse names of methods, allowing the same name to have different meanings in different contexts.
Polymorphism refers to the capability of having methods with the same names and parameter types exhibit different behavior depending on the receiver. In other words, you can send the same message to two different objects and they can respond in different ways.
More generally, the capability of using names to mean different things in different contexts is called overloading. This also includes allowing two methods to have the same name but different parameters types, with different behavior depending on the parameter types. Note that a language could support some kinds of overloading without supporting polymorphism. In that case, most people in the object-oriented community would not consider it to be an object-oriented language.
Polymorphism and overloading can lead to confusion if used excessively. However, the capability of using words or names to mean different things in different contexts is an important part of the power of natural languages. People begin developing the skills for using it in early childhood.
Objects can have their own data, including variables and constants, and their own methods. The variables, constants, and methods associated with an object are collectively referred to as its members or features.
Many object-oriented languages use an important construction called a class. A class is a category of objects, classified according to the members that they have. Like objects, classes can also be implemented in classical languages, using separate compilation and structs for encapsulation. See Implementing a Class in C for a brief explanation and example.
The object-oriented language Java uses the following syntax for class definitions:
public class A { declarations for members }
Each object in the class will have all members defined in the declarations.
In many object-oriented languages, classes are objects in their own right (to a greater or lesser extent, depending on the language). Their primary function is as factories for objects in the category. A class can also hold data variable and constants that are shared by all of its objects and can handle methods that deal with an entire class rather than an individual object. These members are called class members or, in some languages (C++ and Java, for example), static members. The members that are associated with objects are called instance members.
One important characteristic of object-oriented languages is inheritance. Inheritance refers to the capability of defining a new class of objects that inherits from a parent class. New data elements and methods can be added to the new class, but the data elements and methods of the parent class are available for objects in the new class without rewriting their declarations.
For example, Java uses the following syntax for inheritance:
public class B extends A { declarations for new members }
Objects in class B will have all members that are defined for objects in
class A.
In addition, they have the new members defined in the declaration of
class B.
The extends
keyword signals that class B inherits from
class A.
We also say that B is a subclass of A and that A is the
parent class of B.
In some languages, Java for example, the programmer has some control
over which members are inherited.
In Java, a member is defined with a keyword indicating its level of
accessibility.
The keyword private
indicates that the member is
not inherited by subclasses.