Object Oriented Programming
Java is a truly object oriented programming language. Everything in Java is an object. An object is a real world entity which has a state and exhibits a behaviour. A class is a template that describes the states and behaviour of an object. So, an object is simply an instance of a class. The state of an object is maintained by the data members and behaviour is defined by the methods. Please note that the functions in languages like C / C++ are denoted as methods in Java.
Object Oriented Programming Approach
This approach emphasizes more on data rather than methods. Data doesn’t flow freely around the system and are closely coupled to the methods which operate on them. A program to solve a problem is decomposed into entities known as objects and then data and methods are associated with those objects. A system is designed as a combination of objects which consists of related data and methods which manipulate those data. A complex system is easily maintainable and extendible if this approach is followed.
In a procedural programming language, problem is viewed as a sequence of tasks and a number of functions are written to accomplish those tasks. Functions are the primary focus and data is secondary. In simple words, emphasis is more on the algorithm to solve a problem rather than the data on which algorithms operate.
Basic Concepts of Oriented Programming ( OOP )
Objects are the basic entities or building blocks of a system designed using OOP approach. Each object consist of some data and the methods which operates on those data. For e.g, in an Employee Management System , there could be an object named employee. This object can have data like emp_id , emp_salary, serve_period and methods like computeBonus ( ) , increment ( ) which operates on the object data.
Classes are user defined data types which defines the structure of an object. An object is a variable of type class. For e.g, english could be an object of class language.
Data Encapsulation is the wrapping up of data and methods into a single unit called class. Encapsulation binds the data to the methods which operate on them.
Data Abstraction is an act of hiding all the internal details and representing only the essential features of a system. In a class, the data members and the some of the methods which manipulate the data are abstracted and only some methods are exposed to objects of other classes. Abstraction separates implementation and interface. Data members and methods which focus on implementation of some algorithms are abstracted and the methods which are exposed forms the interface through which objects of other classes communicate with the object of this class.
Inheritance provides the concept of code reusability in OOP. While creating a new class, instead of defining completely new data members and functions, we can inherit those members (if they exist) from an existing class. The existing class is known as base class and the new class which inherits from base class is known as derived class. With this feature, we can add new functionalities to an existing class without modifying it.
Polymorphism is the ability of a message to be processed in more than one way. An operation may exhibit different behaviours in different instances depending on the types of data. For e.g, suppose we have an utility to draw a shape. Now, polymorphism provides the ability to draw a particular shape ( rectangle, square, triangle,
etc. ) depending on the context. In Java, polymorphism is achieved using method overloading and method overriding which we will discuss later in the tutorial.