Java – Introduction to Object Oriented Programming [OOP]

In this Java programming tutorial post, we will take an introduction to Object Oriented Programming in Java. Java is an Object Oriented Programming Language. Lets first Understand what exactly is Object Oriented Programming in Detail –

Object Oriented Programming Definition –

OOP is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as a template(Class) for creating copies of such modules on demand (Objects)

To better Understand this definition lets try to see what procedural programming is –

Procedural Oriented Programming –

In procedural programming major focus is on functions rather than data and hence we have programs divided into modules aka functions. Functions are group of program statements with same name used to perform common task (e.g. function named add() might perform addition of 2 or n numbers). In this type however, data is usually global and focus on how to access data is not taken into account. Also this type of programming is at times not suitable for real world scenarios (we will see and example).

Some features of POP –

  • List of instructions for a computer to follow.
  • Group of similar tasks are organized as functions.
  • Most functions shared global data.
  • Data is publicly available.
Object Oriented Programming –

In Object Oriented Programming, focus is given on data and how to access that data and the real world scenarios share more resemblance. Programs are organized in terms of Objects which have the data and functions that access that data. These objects are derived out of Classes (consider them as custom templates that we users can define and there are inbuit classes and objects too).

Some features of OOP –

  • Emphasis on Data rather than the procedure.
  • Programs are divided in to Objects.
  • Data structures are designed such that they characterize the Objects.
  • Functions that operate on the data of an object are tied together in that same data structure.
  • Data is hidden and cannot be directly accesses
Diagramatical Representation of POP vs OOP

java - introduction to Object Oriented Programming

Features of Object Oriented Programming
  • Classes & Objects
  • Data Abstraction and Encapsulation
  • Inheritance
  • Polymorphism
  • Message Passing
Classes & Objects –

A class is a template which consists of data members (variables or other objects) and member functions using which we derived Objects. In simple terms – An Object is a variable of type Class

Class is just a template at we declare (or its predefined). It does not have physical memory, however when we create object of that class, memory is allocated in the RAM. Any entity that has state(variables aka member functions) and behavior(functions aka member functions or methods) is known as an object.

Inheritance –

When one class acquires all the properties and behaviours of parent class i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism. We will study in detail about this in further tutorials.

Polymorphism –

When one task is performed by different ways i.e. known as polymorphism. In terms of programming, a function might behave differently in different scenarios and this functionality is known as polymorphism. In java, we use method overloading and method overriding to achieve polymorphism.

Abstraction –

Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don’t know the internal processing. In java, we use abstract class and interface to achieve abstraction.

Encapsulation –

Binding (or wrapping) code and data together into a single unit is known as encapsulation. A java class is the example of encapsulation. A class typically consists of data members (data) and member functions (code that operate on the data) together.

Advantage of OOPs over Procedure-oriented programming language
  • OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows.
  • OOPs provides data hiding whereas in Procedure-oriented programming language a global data can be accessed from anywhere.
  • OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.
Example of Procedural Oriented Programming vs Object Oriented Programming

java - pop vs oop

Lets understand the difference between POP and OOP and see how OOP is better when it comes to resembling real world scenarios in terms of programming.

Lets assume we want to store details of Cars and store its Model name, fuel type, mileage and price. Assume that we want to store details about 20 cars.

Now in POP since we don’t have Objects and Classes, we have to create individual variables to hold every detail right ? So 5 variables for 1 car (1 variable for name, 1 for, fuel type and so on). Now this is just for 1 car right, we have 20 cars. so 5 x 20 = 100 variables. This definitely makes the whole program messy and lengthy right. Also if we were to peform some more tasks on 1 type or car, we would have to create individual functions for each which again complicates the process. Thus as you can see, in this scenario POP doesn’t seem to be very efficient.

Now lets see what happens in OOP, in OOP we create a class which the required data variables. Then we use this class to create objects. Each object of Cars type has its own copies of variables encapsulated inside them. Thus when we create 1 Car object, by default we create all the data variables that we want to store for that car object. This makes it process easy. Now we can also add methods in the class which takes input from user to store the car details or to print/display those details. This makes it even more efficient as we only have to write the method in the class once and then all the objects of the class will by default get this method. Now there are many other added benefits like inheritance, polymorphism etc etc which we will see in futher tutorials.

This is how OOP is better than POP is many real world problem solving scenarios.

Watch it on YouTube

One thought on “Java – Introduction to Object Oriented Programming [OOP]

  • May 28, 2018 at 12:36 pm
    Permalink

    I’ve been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the internet will be a lot more useful than ever before.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *