Introduction to an Object-Oriented programming language.

Object-oriented Programming language considers the objects and classes as its main components. Every action that is performed in Java is done using its core components, i.e. Object and Classes.

OOPs aims at implementing all the real life concepts like inheritance, polymorphism, abstraction, encapsulation in the programming language.

There are many languages that support OOP’s concepts such as:

  • C#
  • PHP
  • C++
  • Java
  • Python, etc.

We usually hear about Procedural programming language while we talk about Object Oriented Programming. There is a huge difference between both of them, which is explained below:

BasisObject Oriented Programming LanguageProcedure oriented programming language
StructureThe whole program is divided into objectsThe whole program is divided into small functions or modules.
Access ModifiersIt is accessible based on Different Access ModifiersData cannot be accessed by external functions, it is hidden
ApproachIt follows Top Down ApproachIt follows bottom up approach
InheritanceIt supports inheritanceIt does not support inheritance
AbstractionImplements abstraction at class & Object phaseImplements abstraction at function phase
ExamplesE.g. – C++ , C#, Java, JavaScriptE.g. – C , PASCAL , COBOL
Differences between Object Oriented & Procedure Oriented

Advantages of OOP’s over Procedural Programming

  • OOPs is faster
  • OOPs allows reusing the code that is once written
  • OOPs helps in developing the application faster
  • Lower cost of development
  • Improved productivity

Inferring from the above points, we can clearly say that studying the OOPs concept is much more productive as compared to the traditional procedural programming. The motive of OOPs is to make sure that the data and the methods stay together and no other code should be able to access them if they don’t have the required permissions.

Following are the OOP’s concept that are implemented by Java

  • Class
  • Object
  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
Java OOP’s Concepts

Let us elaborate more on the above mentioned concepts:

Class –

Class is a template or a blueprint of an object and the object itself is an instance of a class that represents an entity that has a state and behavior. A class has fields (or say variables) to hold the data and methods to perform certain tasks.

Object

Any kind of real world entity is termed as an Object, for Example – bus, car, table, TV, mobile, etc. i.e., anything that you see around yourself is treated as an object.

An object has different kinds of properties:

  • Behavior – How does the objects responds? The various kind of responses that could be shown by an Object.
  • Identity – A way to uniquely identify an object, which helps it to outstand in the whole project.
  • State – All the attributes, which helps in reflecting the properties of the object.
Representation of Class & Object

From the above diagram, we can clearly see that Class is the main Entity, and each Object has the properties of the class.

Polymorphism

As the name suggests ‘poly’ which means ‘many’ and ‘morph’ which means ‘form’, polymorphism represent a feature of an object to take many forms.

For example, a parent class reference can be used to refer an object of a child class. We can achieve it by ‘method overloading’, where we use the same name of a method but with different number of parameters or the same number of parameters, but they are different either in terms of their type or their order.

It can be achieved in two ways in Java:

  • Java Method Overriding – It occurs when the parent class and the subclass have the same method. Where the subclass is providing some special implementation of the method, it is called as Method overriding.
  • Java Method Overloading – When the method name is the same but it has different signatures, it could either have a different number of parameters or different data type Parameters or both.

Inheritance

It is a feature of an object-oriented programming language that allows any child class (also known as subclass) to inherit all the properties (i.e. fields and methods) of a parent class.

In java, we can use ‘extends’ keyword to inherit the properties from a parent to child class.

Following is the syntax to achieve inheritance in Java:

class SubClass extends ParentClass  
{  
   //methods and fields  
}  


Java Supports the following kinds of inheritance:

  • Single Inheritance: When a Child class inherits features from the parent class
  • Multilevel inheritance: When class C extends features from Class B and Class B inherits features from Class A.
  • Hierarchical inheritance: When more than 1 subclasses extend the same parent class.
  • Multiple Inheritance: It is supported with the help of an interface in Java. 

Abstraction

It is a process of hiding the implementation details from the user. It provides the user the simplicity of using any functionality without knowing its inner working.

In Java, abstraction can be achieved by either:

  • Abstract class or
  • Interfaces

Any class can be made abstract by adding the ‘abstract’ keyword. An abstract class cannot be instantiated. To use it, we need to inherit it from other class.

Consider a simple Example

We all have car remotes, which help us to open or close the car.

Question – But have we ever thought that what is the internal functioning of the remote that helps us to open or close the door of the car.

Answer – No, because all that functionality is hidden from the end user and he/she is given only the required information that is needed.

Thus, the same is implemented in the Abstraction concept.

Encapsulation

The process of binding the data (or say variable) along with methods of the class is known as Encapsulation.

It prevents unwanted access to variables from other classes.

Data hiding is another name for encapsulation. We can encapsulate our data by either declaring a variable which we want to hide as ‘private’ or by using a getter/setter method to access the variable.

The best example of Encapsulation is a Java Class, where we keep both the data variables and the methods together in a class.

Encapsulation in Java

In the above diagram, you can see that the Class is acting as a capsule and holding together the Data Variable and the methods together.

In the coming sections, we will be reading about all these concepts in much more details.

Tejasvi Nanda

About the Author

Tejasvi Nanda