What are Objects in Programming

‘Object’ in programming is often treated as ‘good’ in programming. To know more about Objects and their importance in programming, read this article till the end.

An object in programming is an entity that has variables and programming instructions in the form of functions to operate on that variable is called an object. Thus, the object is a combination or bundle of variables and functions. An object has a state that can be manipulated using functions.

In software programming, Objects are defined to reflect real-world entities. For example, in computer programming, an object can take the identity of a variable, a data structure, a set of programming instructions known as a function or a memory location that can be addressed.

An object in computer programming is associated with object-oriented programming. Thus, objects are a critical component of Object-Oriented technology. Real-world objects such as cats, birds, tables, etc., can be modelled as objects. Each of these real-world objects has two properties one state and second behaviour. 

For example, a dog is a real-world entity with a state that can define the dog. A dog state can be the colour of the dog, name of the dog, and breed of dog. In addition, the dog has its behaviour such as barking and wagging tail of the dog. Another real-world entity is a bicycle. A bicycle has gear, a bicycle has a pedal, and a bicycle has speed. The behaviour of bicycles can be a change of gear, change of pedal, and applying brakes.

To model these real-world entities, programming constructs of procedural programming was not enough. In procedural programming, the main focus is on designing procedures to accomplish the desired task. In procedural programming, a procedure can accept arguments, manipulate them and return results. 

Fortran, Algol-60, and C all are procedural languages. If in a procedural language, square root has to be calculated, then the procedure for that square root can be defined in the following way:

double sqrt_cal(double num)
{
      //programming instruction to calculate square root.
}
void few_fun()
{
     Double sq_root = Sqrt(2);
}

Procedural programming creates functions to accomplish the desired objective.

Data Hiding in Programming

The style of programming has shifted. The shift is observed from procedural style to data organize style. Due to this shift, the size of the program has increased. A set of programming instructions and data they manipulate can be grouped into modules. 

A computer program that is intended to perform specific functionality can be categorized into a particular module. Categorizing programs into modules according to their functionality is known as the data-hiding programming paradigm.

Data hiding can be understood using a stack. To maintain stack two, then operations need to be performed first, push and second, pop. The operation push and pop can be programmed using the user interface. Stack elements can be accessed using this user interface. To use the stack, it has to be initialized.

A stack can be defined in the following manner:

#include “stack.h”

char pop_op( );
void push_op(char);
const stack_size =100;
static char v[stack_size];
static char* p =v;
char pop_op( )
{
   // check for the condition of underflow and then pop the element
}
void push_op(char c)
{
     // check for the condition of overflow and push the element
}

The function push and pop can also be grouped. In this case, a stack can be defined in the following manner:

#include “stack.h”
void use_function( )
{
    char c = pop(push(‘c’));
    if(c!=c)
    {
          error_code(“this is not possible”)
    }
}

In programming languages defined such as Pascal, grouping functions as defined above is not possible. To hide the function’s name from the rest of the program is to define that function within a procedure/module, make that function local to that procedure/module. 

It is possible to group functions and data into a single module. It is the logic of the program that decides which part of the module is visible to the rest of the program.

Data Abstraction in Programming 

A module contains data and functions that act on it. A module can be defined in the following manner:

class stk_id;
stack_id create_stk(int size_stk);
destroy_stk (stk_id);
void push(stk_id, char);
char pop_stk(stk_id);

The above code may be taken as a way of creating type. This technique of creating a type is different from the one that is used to define built-in types. When a type is created by creating modules, then it is different from built-in types. Each module can have its technique of creating a particular type. Following is the example of creating a particular type:

void f( )
{
    stack_stk ss1;
    stack_stk ss2;
  
    ss1 = create_stk(2000);
    
    char c1 = pop_stk(ss1, push(ss1, ‘a’));
    if(cc1!=’cc’) error_stk(“This is impossible”);

    char cc2 = pop_stk(ss2, push(ss2, ‘aa’));
    if(cc2!=’cc’)error(“This is impossible”);

    destroy(ss2); 
}

In the above code push and pop, creating operations are within the function f( ). Thus function f() supports data hiding. The statement stack_stk s11 and statement stack_stk s22 are used to declare two types, s11 and s22. The types ss1 and ss2 obey the definition of stack_stk. 

Abstract Data Type

Programming languages such as C++, Ada, and Clu, .define types act the same way as built-in types. Types declared in this way is also known as user-defined types. The user-defined types can also have user-defined functions that act on user-defined types. For example, if a programmer wants to create a user-defined type having its user-defined functions, then the following is the technique:

class com_plex
{
Double ree, imm;
public:
	com_plex(double rr, double ii)
	{
		ree=rr;
		imm=ii;
}
com_plex(double rr)
{
	ree = rr;
 	imm = 0;
}
friend com_plex operator+ (com_plex, com_plex);
friend com_plex operator-(com_plex);
friend com_plex operator* (com_plex, com_plex);
friend com_plex operator (com_plex, com_plex);
}

The above code is used to declare a class. The class contains data members and member functions. The class is defined as user-defined types. Hence, the class is a user-defined type.

The user-defined type is concrete as it does not change and remains isolated from the rest of the program.

Object-Oriented Programming

Object-Oriented Programming is a programming paradigm that can be used to declare a class. Object-Oriented Programming is used to develop reusable software. The object-Oriented programming technique is an extension of structured programming and abstract data types. For example, an object is often treated as an abstract data type. 

An object-oriented system is used to integrate the code and data into a single unit. An object has a state and well-defined behaviour. Thus, an object reflects real-world entities.

Each object in object-oriented programming follows a particular design pattern. A design pattern may act as a reusable template that can target common problems. There are many design patterns of objects, such as the Function object design pattern; this has a single method. Immutable design pattern object this object is fixed, and it does not change. 

First-class object design pattern, this object can be used without any restriction. Container object design pattern, this object can accommodate other objects. Factory Object design pattern, this object creates other objects. 

Metaobject design pattern, other objects can be created using this object. Prototype Object design pattern, other objects can be created by copying. God Object design pattern, this object has all the functionality. Singleton Object Design pattern, this pattern is used to create one object of the class. 

The object-oriented approach is also used in distributed systems. This is because the objects in the distributed system are service-oriented. For example, an object in the distributed system contains information such as which programming language is used, the type of operating system, and issues related to a particular platform. The distributed objects are based on Object Management Groups CORBA and DCOM of Microsoft.

Other different types of objects include Protocol Object, Replicated Objects, and Live distributed Objects. Protocol Objects are used to do network communication using an Object-Oriented interface. Replicated Objects are used to execute protocols having different configurations and purpose. Finally, live Objects are a generalized form of replicated objects. 

Another application of distributed objects is the Semantic Web. The semantic web is based on the distributed-object framework. In addition, the semantic web uses Web Ontology Language and Resource Description Framework. Resource Description framework contains name object, properties object, attribute objects, and relations object. These objects are internet accessible. Web ontology language contains objects with its principles in set theory and can be implemented using multiple inheritances.

Internet is constantly evolving, and achieving standardization is difficult. Web Ontology Language is used to achieve this standardization. The objects of web ontology language are used to achieve this standardization. Web Ontology Language objects are used to define the application domain using programming languages like Java and C++.

The Objects of Web Ontology Langauge and Object_Oriented Programming are different. The Objects Web Ontology Langauge are dynamic, which means that it regularly undergo a change and can shift to different classes.

All semantic web objects are set to false if and only if the information available is false; otherwise, the objects information value is set to unknown.

Life-Span of Objects

The life cycle of an object begins with the creation of the object and ends with the object’s destruction. The life span of objects depends on the type of language in which the object is created. The lifetime of the object also depends on the program execution life cycle. 

May time Object lifetime coincides with variable lifetime. In Object-Oriented programming languages, memory to object is allocated in a heap, and the lifetime of the objects is determined by the reference made to it. The object is destroyed as soon as the reference is to that particular object is destroyed.

When objects are created in a programming language with a mechanism of static memory allocation, the lifetime of these objects corresponds to the program’s run time. In static memory allocation, objects are created and destroyed according to the sequential creation and destruction statements that appear in the programming construct.

When objects are created in a programming language that follows the mechanism of dynamic memory allocation, the object is created either explicitly or implicitly. The created object is destroyed when the object goes out of scope or when the reference to the object becomes zero.

Class-based Programming

In class-based programming, instantiation is the term given to the mechanism of the creation of objects and object destruction is determined by the constructor or destructor or a finalizer.

A programming language can have both the constructor and the initializer to create an object and a destructor and finalizer to destroy an object. The constructors are class methods, and destructors, initializers, and finalizers belong to the category of an instance method. The constructors and initializers can be parameterized, and destructor and finalizers do not contain parameters.

In the C++ programming language, a garbage collector is a mechanism that automatically calls the finalizer subroutine. 

Objects are also used for resource management. Resources are initialized during the initialization phase and destroyed during the finalization phase. 

To create an object, the size of the object is calculated. The size of the object is the size of the class. Then, the created object can be copied. The object copying can be done using a copy constructor. Finally, the object is copied to make changes in it as desired. In object copying, all the class data members and member functions are copied.

Object copying can be of different types, such as shallow copy or deep copy.

In shallow copy, a new object is created. In this, the new object contains all the fields of the objects from which copying is done. Thus, the shallow copy is better known as field-by-field copy. If the object contains a reference, then this reference is copied to the new object. If the object contains primitive values, then these primitive values are copied to the new object.

Another way of copying an object is deep coying. In Deep coying, a new object is created all the references are dereferenced. So in the deep coying process, a copy is a recursive process. 

There is another version of copy known as lazy copy. A lazy copy is a practical implementation of a deep copy when an object is copied in its initial phase. Then a shallow copy is used.

Java Object

Java is one of the most used Object-Oriented programming languages. As a result, all the Java Objects has indirect references. There is no implicit creation of objects in Objects. Instead, all the objects in Java are reference variables. Java has an automatic memory allocation and de-allocation process, which is maintained by a garbage collection mechanism. 

In Java, an object can be copied using the Java clone( ) method. The clone( ) method returns object. The clone( ) method is an overridable method of Java. The clone( ) method can not be accessed on the abstract type. There is no public clone() method for abstract class in Java. 

Another technique of object copying in Java is the use of a Serializable interface. This method is used in a computer network in case of the wired protocol. 

C#

In C#, reflection is used for the deep coying objects. Using reflection, each object can be copied and being generic, both source object and copied object remain the same.