What is Object-Oriented Programming? The concept of Class and Objects
For example, when computers were first invented, programming was done by toggling in the binary machine instructions using the computer's front panel. As long as programs were just a several hundred instructions long, this way worked. As programs grew, assembly language was invented so that a programmer could deal with bigger, more complex programs, using symbolic representations of the machine instructions. As programs continued to grow, high-level languages were introduced that gave the programmer more tools with which to handle complexity.
The 1960s gave birth to structured programming. This is the approach supported by languages such as C and Pascal. The use of structured languages made it possible to write somewhat complex programs fairly easily. Structured languages are characterized by their support for stand-alone subroutines, local variables, rich control constructs, and their lack of dependence upon the GOTO. Although structured languages are a powerful tool, even they reach their limit when a project becomes excessively large. Consider this: At each milestone in the development of programming, techniques and tools were created to allow the programmer to deal with increasingly greater complexity. Each step of the way, the new approach took the best elements of the previous methods and moved forward.
Earlier to the invention of Object-Oriented Programming, many projects were touching the point where the structured approach no longer worked. Object-oriented methods were created to help programmers overcome these limitations. We can say that Object-Oriented programming is inherited from structured programming. It took the best traits/ideas of structured programming and merged them with several new concepts. The result was a different way of organizing a program. In the most general sense, a program can be organized in one of the two ways:
- around its code
- around its data
Using only structured programming techniques, programs are typically organized around code. This approach can be thought of as "code acting on data." Let's look at an example, a program, written in C (a structured programming language) is defined by its functions, any of which can manipulate data of any type used by the program. Object-oriented programs work the other way around. They are organized around data, with the key principle being "data managing access to code." In an object-oriented language, you define the data and the routines that are permitted access that data. Thus, a data type defines exactly the ways in which that data can be manipulated. To support object-oriented programming, all OOP languages have three features in common: encapsulation, polymorphism, and inheritance. Let's explore each.
Encapsulation is the mechanism that combines the data and the functions which manipulate that data, and keeps both safe from tampering and misuse. In an object-oriented language, functions and data may be bound together in such a way that a self-contained black box is created. We can only give input to and receive output from the black-box without knowing what is going inside the box. When functions and data are joined together in this manner, an object is formed. In other words, an object is the device that supports encapsulation.
Within an object, functions, data or both may be private to that object or public to whole program. Private data of an object is only known to functions of that object and private functions are only accessible within the object. Public members (functions and data) are visible to outside of its objects.
Object-oriented programming languages support polymorphism, which is described by the slogan "one interface, multiple methods." In simple words, polymorphism is the property that allows one interface to control access to a general class of actions
For example, you might have a program that specifies three distinct types of stacks. One stack stores integers, one character and the last floating-point values. You can declare functions with the same names, push() and pop() for all three stacks because polymorphism allows you to. You will create three specific variants of these functions, one for each type of stack, but names of the functions will be the same. The compiler will automatically select the appropriate function based on the data being stored.
Polymorphism helps in reducing the complexity of the program by allowing the same interface to be used to access a general set of procedures. It is the compiler's job to select the specific function as it applies to each state. This selection process is automatic and you, the programmer, do not need to participate. You need only to remember and to utilize the general interface.
Inheritance is the process by which one object of a class can acquire the resources/properties of another object of another class. This is important because it promotes the idea of classification and largest data is made manageable by hierarchical classifications.
You can take biological classification as an example. For example is Human part of mammalia class, which in tern is part of subphylum vertebrata, which is under the phylum chordata, and so on. Animals in each class share some or more common traits. Without the use of classifications, each object would have to define explicitly all of its characteristics. However, through the use of classifications it an object defined only those properties which make is unique in its gener or class. It is inheritance which makes it possible to for an object to be a specific instance of a general class. As you will see in your programming journey, inheritance is an important feature of object-oriented programming.