The OOP Paradigm
The need to program GUIs led to a distinct programming paradigm, called object-oriented programming. An object-oriented programming system (OOPS) consists of objects. An object comprises both data and algorithms. The data describes the state of the object. Objects communicate by sending messages to each other. When an object receives a message, it responds by executing one of its algorithms. The algorithm may change the state of the object, generate output, or send messages to other objects.
A D V E R T I S E M E N T
OOPS are well-suited to programming GUIs. Typically, each element of a GUI is represented by one object. The object determines both the state and the behavior of the corresponding element. For example, an object representing a window would have data for its position, size and title. If the user closes the window, the OOPS sends a message to the window object telling it to close itself. The window then executes an algorithm that erases its image from the screen.
Objects are most important in this style of programming. This contrasts with procedural languages that focus on executing procedures in the computer. Here, you may think of real-world objects such as people, cars, houses, and so on. Common characteristics of these real-world objects are that they all have a state and a behavior. For example, people have state (name, length, weight, etc.) and behavior (talk, walk, sit, ...). Cars have state (speed, direction, fuel consumption, ...) and behavior (start, accelerate, brake, stop, turn, ...).
In an object-oriented language, objects are modeled in the same way: they have state expressed in instance variables and behavior implemented with methods. The methods manipulate the variables that define state. For example, documents on a Website have state (title, URL, content-type) and behavior (open, close, reload, ... ). Screen characters have state (name, font, size, position, ... ) and behavior (draw, resize, italicize, ...).
In an object-oriented language, objects consist of data (instance variables) and operations (methods) to manipulate the data.
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.
Usually, a class represents a person, place, or thing - it is an abstraction of a concept within a computer program. Fundamentally, it encapsulates the state and behavior of that which it conceptually represents. It encapsulates state through data placeholders called member variables; it encapsulates behavior through reusable code called methods.
In object-oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The inheritance concept was invented in 1967 for Simula.
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 existing code with little or no modification.
Inheritance provides the support for representation by categorization 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 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 (replacing) one or more methods exposed by ancestor, or by adding new methods to those exposed by an ancestor.
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
In computer science, the mechanism and practice of abstraction reduce and factor out details so that one can focus on a few concepts at a time. Abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures (e.g. datatypes [vectors and pixels]). Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. object-oriented programming is an attempt to abstract both data and code.
|