Last Updated on July 8, 2020 by Scientech Easy
What Is Object-Oriented Programming in Java?
OOPs Concepts in Java | Object-oriented programming (OOP) in Java is a programming methodology or paradigm (model) to design a computer program using classes and objects.
It is the most popular programming paradigm and widely used in the software industry today. It is an extension of procedural programming.
Procedural programming means writing code without objects whereas, object-oriented programming means writing code with objects which contains data in the form of fields or variables (often known as attributes or properties in java) and functionality code in the form of methods (often known as behavior in java).
OOP concept uses variables and methods as procedural programs do, but it focuses on the objects that contain variables and methods.
That’s why this new approach is called object-oriented approach in which we write programs using classes and objects. It comes with four main features like encapsulation, abstraction, inheritance, and polymorphism.
When we write a program using these features, it is called Object-Oriented Programming System (OOPs). The main goal of the OOPs concept in java programming is that everything you want to do, do through objects.
1. Not all languages support OOPs concepts. For example, languages like C, Pascal, Fortran, etc does not implement OOPs features because these are Procedure oriented programming. In these languages, a programmer uses procedures or functions to perform a task.
2. Languages that implements OOPs features are Smalltalk, C++, Java, PHP, Python, and C#. C++ is a procedural language with object-oriented extension. Java is a pure object-oriented language. Smalltalk is the first truly object-oriented programming language.
Object-Oriented Programming Paradigm (Model/Design)
Object oriented programming is a new approach to overcome the drawbacks of a procedural oriented approach. It divides programs into the number of entities called objects that contain data (variables) and functions/tasks (known as methods in java).
In an application, objects communicate with each other, they share data among them and solve the problems. The combination of data and functions (methods) in OOP makes an object that is shown in the below figure.
The data of an object is tied more closely with a function that operates on it. In the OOPs concept, data cannot move freely around the system.
The data of an object can be accessed and modified only by the function of that object. An external function cannot access the data of an object but the functions of one object can access the function of other objects.
Thus, data is protected and safe from the modification by external functions. In this way, data is hidden in the object-oriented programming language.
Features of OOPs in Java
Some of the striking features of object-oriented programming system (OOPs model/design) are as follows:
1. Higher priority is focused on Data rather than functions.
2. Programs are divided into the number of entities known as objects.
3. Objects communicate with each other through functions (methods).
4. Methods that operate on Data of an object are closely bound together in the data structure.
5. An object is a group of data and methods.
6. Data is hidden in the OOP and cannot be accessed by external methods. Hence, it is safe from accidental modification.
7. Data cannot move freely out of the object.
8. New data and methods can be easily added whenever needs.
9. A bottom-up approach is adopted in programming design.
Basic OOPs Concepts in Java with Realtime Examples
Let’s understand the basic concepts used extensively in object-oriented programming. They are as follows:
1. Object: A real-world entity that has state and behavior is called object in java. Here, state represents properties and behavior represents actions and functionality. For example, a person, chair, pen, table, keyboard, bike, etc.
Let’s take a realtime example to understand the characteristics of an object. A person has three characteristics: Identity (name), State (properties), and behavior (actions or functionality).
The state/properties of a person are black hair, black eyes, white skin, 6 feet tall, etc. The actions or behavior of the person may be “eat, sleep, walk, play, and study”.
2. Class: A class is basically user-defined data types that act as a template for creating objects of the identical type. It represents the common properties and actions (functions) of an object.
For example, bus and car are objects of vehicle class. Sparrow and parrot are objects of birds class. Similarly, MS Dhoni, Sachin Tendulkar, and Virat Kohali are objects of cricketer class.
Object takes space in the memory but class does not take any space in the memory. Class does not exist physically but an object exists physically.
3. Encapsulation: The process of binding data (variables) and corresponding functions (methods) together into a single unit (called class) is called encapsulation in Java. It is one of the striking features to achieve data security in an object-oriented program.
Through encapsulation, data is hidden and protected from access by outside non-member methods of a class. Only member methods defined in a class will have access to the data.
A Java class is an example of encapsulation because class binds variables and methods together.
Generally, variables of a class are declared as “private” whereas, methods of class are declared as “public”. This means that variables cannot be accessed from outside the class but methods can be accessed from anywhere outside the class.
To use variables from outside, we will have to take the help of methods. Thus, encapsulation protects data of a class from members of another class. The reason is that when we create an object of class, each object shares different memory and thus, overwriting of data is not possible.
4. Abstraction: Abstraction is a technique by which we can hide the unnecessary data that is not needed from a user and expose only that data is of interest to the user. It hides all unwanted data so that users can work only with the required data.
Abstraction is one of the fundamental principles of object-oriented programming that permits the user to use an object without knowing its internal details. It helps to reduce complexity by not including background details.
A realtime example of abstraction is “sending SMS”. When you need to send SMS from your mobile, you only type the text and send the message. But you don’t know the internal processing about the message delivery.
5. Inheritance: Inheritance is one of the main pillars of the OOP concept. It provides a mechanism for the users to reuse the existing code within the new applications. It does this by allowing the programmer to build a relationship between a new class and existing class and define a new code in terms of existing code.
The technique of constructing a new class by using an existing class functionality is called inheritance.
In other words, Inheritance in Java is a process where a child class acquires all the properties and behaviors of the parent class. The existing class is called parent class and the new class is called child class.
Let’s take a realtime example to understand inheritance process. In the below figure, course is a parent class from which two child classes professional and non-professional courses are derived.
The courses B.Sc, B.Com, and BA are child classes that are derived from a non-professional course class. Similarly, B.Tech, MBA, and MBBS are child classes derived from professional course class as shown in the below figure.
6. Polymorphism: Polymorphism is another salient feature of OOP concepts in which method can take more than one form based on the type of parameters, order of parameters, and number of parameters.
It is a concept by which we can perform a single task in different ways. That is, when a single entity behaves differently in different cases, it is called polymorphism.
Using polymorphism, we can achieve flexibility in our code because we can perform various operations by using methods with the same names according to requirements.
The best real-life example of polymorphism is human behavior. One person can have different behavior. For example, a person acts as an employee in the office, a customer in the shopping mall, a passenger in bus/train, a student in school, and a son at home.
7. Dynamic Binding: Dynamic binding (also known as late binding) is a way of connecting one program to another that is to be executed whenever it is called at runtime. It is associated with inheritance and polymorphism.
8. Massage Passing: In OOP, objects communicate with each other through member methods of the class. To establish communication between two objects, the following steps take place:
a. Creating classes that declare variables and methods.
b. Creating objects of classes that already declared.
c. Calling methods through suitable data to establish communication between objects.
Message passing involves three elements: name of object, name of method, and information to be sent. For example, consider the below statement.
Here, Employee is name of object, salary is the name of method and name is parameter that contains information.
Advantage/Benefit of OOPs Concepts in Java
OOPs concept in Java offers several advantages that are not available in procedural programming like C, Pascal, etc. Some of the major benefits of object-oriented programming in java are as follows:
1. Security: In OOP, Data is encapsulated with methods in the class so that data is protected and secured from accidental modification by other external non-member methods.
2. Reusability: Through inheritance, we can use the features of an existing class in a new class without repeating existing code that saves a lot of time of developers and also increases productivity.
3. Effective communication: In OOP, objects can communicate via message passing technique that makes interface descriptions with outside systems much simpler.
4. Developing complex software: OOPs is the most suitable approach for developing complex software because it minimizes the complexity through the feature of inheritance.
5. Easily upgraded: Object-oriented system can be easily upgraded from small to large systems because OOP uses bottom-up approach.
6. Easy partition of work: It is easy to partition of complicated work in a project based on objects.
7. Maintenance: The maintenance of object-oriented code is easier.
8. Efficiency: The concepts of OOP provides better efficiency and easy development process.
Application of OOPs concepts in Java
The concepts of OOPs provides many benefits for the programmer to design an efficient program. Due to its reusability feature, it is widely used in many areas. Some of the application areas of OOP are as follows:
a. Real-time systems
b. Object-oriented database
c. Graphical user interface design in the Windows operating system.
d. Artificial intelligence and expert systems
e. Parallel programming
f. CAD/CAM software and in many areas.
Hope that this tutorial has covered important points related to OOPs concepts in java with realtime examples. Keep in mind the following key points about object-oriented programming system.
Key Points to Remember:
1. Encapsulation, Abstraction, Inheritance, and Polymorphism are the main fundamental principles (main pillars) of the OOPs concepts in Java.
2. Object oriented programming model is an extension of procedural programming. It designs the computer program using classes and objects.
3. Java OOPs concepts provide several advantages such as security, reusability, effective communication, developing complex software, maintenance, and efficiency.