Classification of Object-Oriented Programming Concepts.
7. Dynamic Binding
8. Message Passing
In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an instance of the class of objects known as bicycles. Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle’s state is independent of and can be different from that of other bicycles.
When building bicycles, manufacturers take advantage of the fact that bicycles share characteristics, building many bicycles from the same blueprint. It would be very inefficient to produce a new blueprint for every individual bicycle manufactured.
In object-oriented software, it’s also possible to have many objects of the same kind that share characteristics: rectangles, employee records, video clips, and so on. Like the bicycle manufacturers, you can take advantage of the fact that objects of the same kind are similar and you can create a blueprint for those objects. A software blueprint for objects is called a class.
Definition: A class is a blueprint that defines the variables and the methods common to all objects of a certain kind.
An object is a software “bundle” consisting of a set of variables that define the states the object can exist in and a set of functions that define the behavior of that object. Software objects are often used to model the real-world objects that you find in everyday life.
The “objects” used to create an OOP program are classes and structures. A class groups together a number of related fields (member variables) of different data types which define the state of the object, and a number of methods (functions) that define the behavior of the object and ways of modifying the object’s state.
Characteristics of Objects
- State – An object stores its state in fields, i.e. member variables in classes.
- Defined behaviors – An object expresses its behavior through methods (functions in some programming languages). Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication. Hiding the internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation which is a fundamental principle of object-oriented programming.
- Defined ways of modifying the state – Only an object’s methods should be able to modify its’ state, i.e. none of the fields should be directly modifiable by other object’s code.
Encapsulation is the process of enclosing all critical information inside an object and only revealing a subset of it to the outside world. For example, code inside the class template defines attributes and behaviors.
The data and methods are then enclosed in the object when it is created from the class. Inside a class, encapsulation conceals the underlying software code implementation and the internal data of the objects. Encapsulation necessitates designating certain fields as private while others are made public.
- Methods and attributes only available from other methods in the same class make up the private/internal interface.
- Methods and attributes that are available from outside the class are known as the public / external interface.
Encapsulation Demonstration in Real-Time
One of the most practical examples of encapsulation is a school bag. Our books, pencils, and other items may be kept in our school bags.
Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of the car or applying brakes will stop the car but he does not know how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is what abstraction is.
- Abstraction using Classes: We can implement Abstraction in C++ using classes. The class helps us to group data members and member functions using available access specifiers. A Class can decide which data member will be visible to the outside world and which is not.
- Abstraction in Header files: One more type of abstraction in C++ can be header files. For example, consider the pow() method present in math.h header file. Whenever we need to calculate the power of a number, we simply call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the underlying algorithm according to which the function is actually calculating the power of numbers.
Polymorphism is one of the core concepts of object-oriented programming (OOP) and describes situations in which something occurs in several different forms. In computer science, it describes the concept that you can access objects of different types through the same interface. Each type can provide its own independent implementation of this interface.
Different types of polymorphism
Java supports 2 types of polymorphism:
- static or compile-time
Inheritance is the procedure in which one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class. And the class that inherits the properties from the parent class is the Child class.
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
Super Class: The class whose properties are inherited by a sub-class is called Base Class or Superclass.
Dynamic binding called dynamic dispatch is the process of linking procedure call to a specific sequence of code (method) at run-time. It means that the code to be executed for a specific procedure call is not known until run-time. Dynamic binding is also known as late binding or run-time binding.
Static Binding and Dynamic Binding
Connecting a method call to the method body is known as binding.
There are two types of binding
- Static Binding (also known as Early Binding).
- Dynamic Binding (also known as Late Binding).
Objects communicate with one another by sending and receiving information from each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function, and the information to be sent.