Java OOPs Concepts


In this section we will learn the basics of object oriented programming in Java. The object oriented programming in Java programming language provides concepts which include inheritance, data binding, encapsulation, polymorphism, etc.

The very first object oriented programming introduced was called as Simula. In object oriented programming everything is represented or considered as an object. Smalltalk was another truly considered object oriented programming in which everything is considered and represented as objects.


Object Oriented Programming System (OOPs):

In real life an object is something that can be touched for example a table, a chair or pen all are objects as they can be touched. In object oriented programming the programs or codes are written in the form of objects and classes. The following are the concepts that are used in a program in object oriented programming which simplifies our program and program becomes easy to maintain:

  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation



An object is an entity that has some state or behavior or that can be touched or that is logical. For example, a table, a pen, a chair, etc all is objects as they are in some state and they can be touched. An object can be physical or it can be logical.



A class is a logical entity which contains objects. We can say that a class is a collection of objects.



Inheritance is an important feature of Java programming language. Inheritance uses a concept Generalization which means extraction of class from generic class. The generic class is the main class or the parent class of the program. Inheritance means to make objects out of other objects. Consider the following diagram to understand the concept Inheritance:


The class person is the generic class. In inheritance the objects of the child class have all the properties of the objects of the parent class. The code can be reused if it has used the concept of inheritance. The concept of inheritance is used in programs to achieve the run time polymorphism.



In object oriented programming, polymorphism is a concept that can process the objects differently with respect to the data type or the class of the objects. Polymorphism redefines the methods for derived classes or child classes.

In other words we can say that through polymorphism we can perform a number of tasks in different ways. To achieve polymorphism in Java programming language we use overloading and overriding of functions or methods.

Polymorphism is used in class inheritance. That is this concept is applied to base classes and derived classes. You should have a basic knowledge of pointers to understand polymorphism. Let us look at a simple example:


Here shape is the base class and circle, triangle and square are derived classes.



Abstraction is a process in which we are exposing the interfaces and hiding the implemented data or details from the user. A pure virtual function is necessary or it is a condition for a class to be abstract. An abstract class can have more than one pure virtual function.

If a class has more than one function, the derived class must define every pure virtual function in its base class; if not so it will also be an abstract class. We can also create our own data types using data abstraction and then define objects of these data types. Through data abstraction the user can define the set of operations on their own created data types. Data abstraction is also used to easily modify the program.

Data abstraction is also used to hide the data from the user or to hide the data of the structure. The data can be hid by using the access specifiers. There are three types of access specifiers in object oriented programming; private, protected, and public. Usually the data members in a class are kept private and in the parent class or the base class the data members are kept protected. The member functions or methods are always public, so that they can be used anywhere in the scope of the program.

In short, data abstraction is a concept that only shows the necessary details to the user and hides the data.

Abstract class and interface is used to achieve abstraction in Java programming language.



Encapsulation is an object oriented programming concept that hides the personal data from outside world using the access specifiers; private and protected. Encapsulation basically means to protect the data in a class using private and protected. Data encapsulation is also called data hiding. Data hiding is where we do not want the user to have the access of the back end details or the details of the background of the program.

To bind the code with the data together in one single unit is called data encapsulation.


Advantage of OOPs over Procedure Oriented programming language:

The following are the advantages of object oriented programming over procedural programming language:

  1. In object oriented programming it is easy to main large programs but in procedural programming language large programs are difficult to maintain and manage.
  2. In object oriented programming data can be hidden from outside world but in procedural programming language data can be accessed from anywhere.
  3. The real world problems can be solved by using object oriented programming more effectively.
  4. In object oriented programming data is given more importance rather than functions and the functions work as in real world but in procedural programming the sequence of functions is more important than data.
  5. In object oriented programming we have access specifiers named: public, private and protected. In procedural programming language there are not access specifiers.
  6. In object oriented programming new functions and data can be added more easily but it cannot be done easily in procedural programming language. It means that a program can be expanded easily when using object oriented programming.
  7. Overloading of function and operators is possible in object oriented programming but not in procedural programming language.


What is the difference between Object oriented programming and object based programming language?

In object based programming language we cannot use inheritance and remaining all features of OOPs are not supported. The best example of object based programming language is JavaScript and VBScript.