What is Object Oriented Programming?
Object Oriented Programming (OOP), in computer science is a type of high-level computer language that uses self-contained, modular instruction sets for defining and manipulating aspects of a computer program.
These discrete, pre-defined instruction sets are called objects and may be used to define variables, data structures, and procedures for executing data operations. In OOP, objects have built-in rules for communicating with one another. They can also be manipulated or combined in various ways to modify existing programs and create entirely new ones from pieces of other programs. It should be clear that class and inheritance properties of objects are ideal for certain types of knowledge representation. In fact, structured objects were used in knowledge representation schemes before the introduction of OO as a software engineering paradigm. So much information about the world can be represented by identifying objects and their related attributes (or associated data structures).
|Person||Age, sex, name, …|
|Circle||Center coordinates, radius|
|Car||Top speed, make, color …|
Object Oriented Programming is a new way of approaching the job of programming. Approaches to programming have changed dramatically since the invention of the computer in order to accommodate the increasing complexity of programs. Object oriented programming takes the best ideas of structured programming and combines them with powerful, new concepts that encourage users to look at the task of programming in a new light. Object oriented programming allows the easy decomposition of a problem into subgroups of related parts and translates these subgroups into self-contained units called objects. All object-oriented programming languages have three things in common. These are objects, inheritance, and polymorphism.
The key ideas of the object oriented approach are:
- Class and Inheritance
- Methods and Messages
The position of Object Oriented Programming or language in computer languages is as shown in the figure below:
An object is one of the building blocks of object oriented programming. It is a real-world entity that has existence and importance. For example Pen, car, and person. In the case of object oriented programming, every object must have an identity that will differentiate it from other objects. A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.
An object is a combination of data and functions.
The main principle in the object oriented (OO) approach is that of abstraction, not of data structures and processes separately but both together. An object is a set of data structures and the methods or operations needed to access those structures. The two important features of an object are:
- Every object must have a unique identity.
- Being a runtime entity it will consume memory.
Encapsulation of data structures and methods means that only the methods associated with the object can access the internal data structures. In other words, it is a mechanism that is used to bind together the data and functions such that it behaves like a single entity. With the help of encapsulation, one can save the data from any external interference and misuse. Now the data in an object can be accessed only by the member function bound with the data only. No other function can access the data, the advantage of the encapsulation.
Class is one of the major features of object oriented programming. When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
A class is defined as a collection of objects that has common attributes and methods. Technically it acts as the blueprint for creating objects.
For example, consider a class in which all students are of the same level. The various attributes of student class can be: Rollno, Name, Fees, Grade and the behavior, methods, or functions of a class can be admission, test, and result as shown in the following diagram:
From the above student class, various objects like Student (A), Student (B), and Student (C) can be created. All the object will share the same data member (rollno, name, age) and methods (admission(), test(), result()).
Difference between object and class
A class is a predefined frame of an object, in which it’s mentioning what all variables and methods an object (object of that class) can have. The various differences between class and objects are as follows:
|1.||It is the collection of similar types of object that has the same attributes and methods.||It is the working instance of a class.|
|2.||It is a logical entity that does not come into the real picture while processing.||It is the real-world entity that is actually processed such as a pen, laptop, mobile, bed, keyboard, mouse, chair, etc. It is a physical entity.|
|3.||Being a logical entity it does not consume memory.||Being a processed entity it consumes some memory depending upon the data members.|
|4.||It is another kind of data type in OOPS like structure or union in C language. It is static in nature during program execution.||It is like a variable of a type class for accessing member variables and methods. Objects are dynamic in nature during program execution. They can be constructed and destroyed.|
|5.||Example: Student is a class having rollno, name, and age as attributes and admission(), test() as the method.||Example: Student, car, bike.|
|6.||The class has no existence in computer. When a class is declared no space in the memory is allocated for it.||But the object has its own space allocated in the memory. When an object of a class is declared, the system has to allot a memory to the object.|
Another excellent feature of classes is the ability to inherit properties and procedures from another class.
|The same is not done with objects, as there is really no practical use for globally inheriting an object’s data.
The basic practice for programmers is to create a subclass and create the object from the subclass.
|8.||Class is a blueprint used to create objects.||The object is an instance of a class.|
|9.||Class is generated from Objects.||Objects generate a class or classes.|
|10.||It depends upon the Object(s).||It depends upon the given problem.|
|11.||Class is declared using class keyword e.g. class Student()||An object is created through a new keyword mainly e.g. Student s1 = new Student();|
|12.||Class is declared once.||The object is created many times as per requirements.|
One especially powerful feature of OOP languages is a property known as inheritance. Inheritance allows an object to take on the characteristics and functions of other objects to which it is functionally connected. Programmers connect objects by grouping them together in different classes and by grouping the classes into hierarchies. These classes and hierarchies allow programmers to define the characteristics and functions of objects without needing to repeat source code, and the coded instructions in a program.
Thus, by using OOP languages time taken by a programmer to write an application and the size of the program can be greatly reduced. OOP languages are flexible and adaptable, so programs or parts of programs can be used for more than one task. Programs written with OOP languages are generally shorter in length and contain fewer bugs, or mistakes, than those written with non-OOP languages.
The instance is the running object on which the data manipulation is done. Methods are also known as member functions that are used to perform tasks on the data created in an object or class. On the other hand, messages act as the carrier for object communication, that is two objects can communicate with one another with the help of message passing.
A number of benefits are provided by the objects when they are used in the code:
- Ease of debugging – The object can be easily removed from the code if there is some problem due to it. A different object can be plugged in as a replacement for the former one.
- Information hiding – The code or internal implementation is hidden from the users when the interaction is done through the object’s methods.
- Reuse of code – if an object or code is written by some other programmer then you can also use that object in your program. In this way, objects are highly reusable. This allows experts to debug, and implement task-specific and complex objects that can be used in your own code.
- Modularity – You can write as well as maintain the source codes of objects in an independent manner. This provides a modular approach to programming.
Disadvantages of Conventional programming and solutions in OOPs
Conventional programming is writing a program in a traditional procedural language such as assembly language or a high-level compiler language like Fortran, COBOL, Pascal, C, C++, etc. The use of conventional or standard programming languages, as opposed to application development languages, financial planning languages, query languages, and report programs. Following are the major disadvantages or cons of simple programming and its solution in Object Oriented Programming as described:
> Emphasis on function as compared to data:
Conventional Programming uses high-level language which is also called procedural language or structure Programming language, the primary focus is on the function. In structured programming, a program has a fixed well-defined structure.
> Data items are global:
Data items are linked directly in the conventional programming in a global system. In multifunction programs many data items are placed as global, these data items are accessed by all the functions. Some data items are local to a function. i.e. these data items are accessed by only the function in which they are defined. In structured programming most of the data items are global, if the program is too large, then it is very difficult to identify what type of data is used by which function. Another serious drawback of structured programming is that it does not model real-world problems.
OOPS solve these drawbacks. i.e. in object-oriented programming data and functions (which operate and handle the data) are organized in one entity called class.
Another weakness of procedural language is its complexity which OOPS is able to cope with ease which means when programs become large; a single list of instructions becomes complex and lengthy. Some Programmers use a modular technique or function which makes structural Programming easier. It means if there are more than a hundred statements, then they can be broken down into smaller units.
A Program is divided into functions and each function has a clearly defined purpose and interfaces with other functions in the Program. This idea of breaking a program into functions can be extended by grouping a number of functions together into a large entity called a module. So dividing a program into functions and modules is known as structured Programming. But when the program is too large and complex, then the concept of structured Programming is useless, and OOPS works efficiently.
A more serious problem with procedural language is the abstraction that does not model real-world problems very well because functions used in the procedural approach are action-oriented and are not interfaced with the components or elements of the problem.
The objectives of OOPs are clear, reliable, and easy-to-understand programs. By using OOPs, complexity can be removed by giving names to each module/function and every module/function is lined and interface with each other properly. So larger programs can be managed clearly and efficiently. It may be noted that the OOPS concept makes the programming easier as compared to the previously used procedure-oriented language.
Difference between Procedure Oriented Programming and OOPs
A program in the procedural language is a list of instructions where each statement tells the computer to do something. It focuses on the procedure (function) & algorithm needed to perform the derived computation. When the program becomes larger, it is divided into functions & each function has a clearly defined purpose. Dividing the program into functions & modules is one of the cornerstones of structured programming. Procedural programming guides the application through a sequence of instructions and focuses on the idea that all algorithms are executed with functions and data that the programmer has access to and is able to change.
Characteristics of Procedural oriented programming:
- It focuses on process rather than data.
- It takes a problem as a sequence of things to be done such as reading, calculating, and printing. Hence, a number of functions are written to solve a problem.
- A program is divided into a number of functions and each function has a clearly defined purpose.
- Most of the functions share global data.
- Data moves openly around the system from function to function.
Drawbacks of Procedural oriented programming (structured programming):
- It emphasizes doing things. Data is given a second-class status even though data is the reason for the existence of the program.
- Since every function has complete access to the global variables, the new programmer can corrupt the data accidentally by creating a function. Similarly, if new data is to be added, all the functions needed to be modified to access the data.
- It is often difficult to design because the components’ function and data structure do not model the real world.
For example, in designing a graphical user interface, we think about what functions, and data structures are needed rather than which menu, menu item, and soon. It is difficult to create new data types. The ability to create a new data type of its own is called extensibility. Structured programming languages are not extensible. To overcome the limitation of Procedural oriented programming languages Object-oriented programming languages were developed.
Object-Oriented programming is much more similar to the way the real world works. Each program is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Objects cannot directly access another object’s data. Instead, a message must be sent requesting the data, just like people must ask one another for information.
Benefits of Object-Oriented programming include:
- Programmers are able to develop the solution faster.
- Programmers are able to produce more accurate and better-written applications compared with a procedural program.
- Ability to simulate real-world events much more effectively.
- Code is reusable thus less code may have to be written.
- Able to create GUI (graphical user interface) applications.
- Code is more efficient.
- Code of OOP is maintainable.
Advantages and Disadvantages of Procedure Oriented Programming and OOPs
The various differences between procedure oriented and Object oriented programming languages are written in the below table:
|S. No.||Procedure Oriented Programming||Object Oriented Programming|
|1.||It emphasizes an algorithmic approach. (Sequence of steps)||It emphasizes real-world items like data and objects.|
|2.||It uses function programming and a modular approach. Here large programs are divided into smaller programs (called functions)||It uses an object approach. Here large programs are divided into smaller modules (called objects).|
|3.||Here most of the functions share global data.||Here every object can share global as well as local data.|
|4.||Data is in the shareable mode and can be moved from one function to another function which means any external function can access the data.||Data is in non-shareable mode and is hidden. Data cannot be accessed by external functions.|
|5.||It uses a top-down approach in program design.||It uses a bottom-up approach in the program design.|
|6.||Function communicates by transforming data from one form to another.||Objects communicate with each other through functions.|
|7.||Examples are C, Fortran, Basic, Pascal, etc.||Examples are C++, Java, Python, C#, etc.|
|8.||It is very difficult to handle long and complex programs.||It is very simple to handle long and complex programs with the OOPs concept.|
|9.||It does not emphasize what to do to solve a problem||It emphasizes what to do to solve a problem as functions are interfaces to the components or elements of the problem.|
|10.||It is used for designing medium size problems.||It is used for designing large problems.|
|11.||Functions communicate with each other by passing the parameters.||Messages are passed onto objects.|
|12.||It uses procedure abstraction.||It uses data abstraction.|
|13.||It is less reliable.||It is more reliable.|
|14.||It is efficient.||It is highly efficient.|
|15.||It is portable.||It is highly portable.|
|16.||Less linkage with Java and dot net technology.||Fully linked with Java and advanced dot net techniques.|
|17.||Compatible with lesser software.||Compatible with all the software.|
|18.||It is a structured language.||It is structures as well as object oriented language.|
So OOPs aid in better program design, data hiding, code reusability, transparent conceptualization, promotes efficiency and modularity, and reduce complexity. All object oriented programming languages have three things in common: Object, Polymorphism, and Inheritance. So the fundamental idea behind Object-oriented language is to combine different sets into a single program entity, both data and the functions that operate on that data. Such an entity is called an object.
When a Programming problem is approached in an object oriented language, it is asked to solve the problem by dividing it into objects. The approach is in items of objects rather than functions. Some items that become objects in object-oriented programs can be physical objects. The corresponding match between programming objects and real-world objects is the result of combining data and member functions. Many real-world objects have both a state and abilities. In object oriented approach, an object’s data records its state, and its member functions correspond to its abilities.