Object Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming technique that revolves around the concept of “objects”. Objects are nothing but real-world entities around us, like students, birds, mountains, chairs, etc. By utilizing OOP, developers can create applications at a higher level, where the focus shifts from controlling the flow of execution to objects interacting with one another in predefined ways. This approach allows programmers to create new programs or systems by combining software objects. This article is a continuation of the learning Java series. 

In the real world, objects are ubiquitous and can be classified into various categories. For instance, humans, vehicles, libraries, rivers, watches, and fans are physical objects, while logical objects include examination results and bank accounts. Each real-world object possesses distinct characteristics and behaviours. For instance, humans have characteristics such as gender, age, height, weight, and complexion, while their behaviours encompass walking, eating, working, reacting, and more.

The object-oriented philosophy suggests that the things manipulated by the program should correspond to things in the real world.

  • Classification is called a Class in OOP
  • A Real-world entity is called an Object in OOP
  • Characteristic is called Property in OOP
  • Behavior is called a Method in OOP

Class in OOP

A class is the building block in Object-Oriented Programming. It can be defined in multiple ways:

  •      A class is a blueprint for an object.
  •      A class is a user-defined data type.
  •      A class is a collection of objects of the same kind.
  •      A class is a user-defined data type that combines data and methods.

A class describes both the data and behaviours of objects. It contains data members (also known as field, property or data) and member functions (also known as method, action or behaviour).  An object is an instance of a class and can be thought of as a variable that belongs to a specific class. It serves as a data structure combining data and functions within a single entity. Objects in object-oriented programming (OOP) are akin to real-world entities, representing the basic runtime entities in the program.

The following figure shows some common real-world classes with their properties and behaviours.

Class in Object Oriented Programming
Class in Object-Oriented Programming

Object in OOP

In the Java programming language, class variables are commonly referred to as objects. We can access the member variables and member functions defined within a class by utilising objects. Objects represent various entities, such as people, places, or items that a program interacts with. 

For instance, if we have a class called “Country,” the objects of that class can include Pakistan, China, Japan, the United States, and so on. A single class can create any number of objects, as shown in the following figure.

Creating object from a class
Creating an object from a class

The following table shows some real-world class and their objects. Note that you can create as many objects of a class a many you want. But every object will have its own set of attributes and functionalities.

Some classes and their objects

PersonAllen Turing, Elon Musk, Nusrat Fateh Ali Khan
UniversityIslamia University of Bahawalpur, Punjab University, Comsats University
BookQuran Majeed, C++ in hand, Let us C, Rich Dad Poor Dad
StudentAqsa, Ali, Chris, Nadeem, Shakeel, Usman Ch.
Cold DrinksCoca Cola, Pepsi, Marinda, Gourmet Cola, Next Cola
Social MediaFacebook, Twitter, Instagram, LinkedIn
FeeTuition Fee, Examination Fee, Check-up Fee
Table: Some real-world classes with their objects

Class vs Object

The following table shows the difference between class and object.

1. For a single class, there can be any number of objects. For example – If we define the class as River then Sutluj, Ravi, and Sindh can be the objects of the class River.1. there can be any number of objects for a single class. For example – If we define the class as River, then Sutluj, Ravi, and Sindh can be the objects of the class River.
2. The scope of the class is persistent throughout the program.The objects can be created and destroyed as per the requirements.
3. The class can not be initialized with some property values.We can assign some property values to the objects.
4. A class has a unique name.Various objects having different names can be created for the same class
Table: Class Vs. Object

Four Pillars of Object-Oriented Programming

Four pillars of OOP provide real power to OOP.       

  1. Abstraction
  2. Inheritance
  3. Polymorphism
  4. Encapsulation

Let us discuss these one by one

1.   Abstraction 

Abstraction is a key concept in OOP that involves presenting only relevant data and hiding unnecessary details from the user. It allows users to interact with objects without being concerned about the underlying implementation. For example, when logging into a Facebook account, users enter their credentials and click “login” without knowing how the data is sent to the server or verified. Abstraction simplifies complex processes, allowing users to focus on essential tasks and improving code maintainability and user experience.

Let’s explore another example of abstraction. Consider a car object. It consists of smaller components like the gearing system, steering mechanism, and engine, each of which has its own subsystems. However, for an ordinary person, a car is just a singular object that can be operated effectively through its subsystems. He will not consider about internal subsystems of the car. In this way, abstraction plays a crucial role in simplifying complex real-world systems. In Java programming, abstract classes and interfaces are utilized to achieve abstraction. In Chapter 7, we will delve into a more comprehensive discussion of this concept, providing a deeper understanding of how abstraction is applied in Java and its significance in implementing real-world systems.

Abstraction in java.png
Abstraction in java.png

2. Inheritance

Inheritance involves the concept of the parent-child relationship. In inheritance, the child object gets all the properties and behaviours of the parent object, plus it also has its own properties and behaviours. It provides code reusability. Inheritance is used to achieve runtime polymorphism. In Java, we extend other classes and implement Interfaces to achieve inheritance. We will discuss inheritance in detail in the coming articles.

Inheritance in Java
Inheritance in Java

3. Polymorphism

When one task can be performed in multiple ways, i.e. known as polymorphism. For example, to convince the customer differently, to draw something, e.g., a shape or rectangle, etc.

In Java, we use method overloading and method overriding to achieve polymorphism. Another example can be to speak something, e.g. cat speaks mew, a dog barks woof, etc.  Figure showing how the same person behaves differently in different situations.

Polymorphism in Java
Polymorphism in Java

4. Encapsulation

Encapsulation refers to the binding or wrapping of code and data into a single entity. It is like a capsule that contains various medications. In the context of Java programming, a class exemplifies encapsulation. A Java bean serves as a prime example of a fully encapsulated class. The intricate details of encapsulation will be thoroughly explored in Chapter 6, providing a comprehensive understanding of this concept. 

Encapsulation in java
Encapsulation in java

Object Oriented Programming (OOP) in Java

As both a high-level language and platform, Java has gained immense popularity as one of the most widely used object-oriented programming languages. It offers a secure, robust, and powerful environment for developing various software applications. In the context of programming, a platform refers to the hardware or software used to execute programs. Java, with its own Application Programming Interface (API) and runtime environment (JRE), can be considered a platform in itself.

According to Sun, approximately 3 billion devices run Java, highlighting its widespread adoption. It is utilized in developing diverse applications, ranging from desktop applications such as PDF readers, media players, and anti-virus software to web applications powered by the mighty Java language. Enterprise applications, characterized by their distributed nature, such as banking systems, also heavily rely on Java for their development. Additionally, Java finds applications in mobile app development, embedded systems, gaming, smart cards, and robotics technology.

To illustrate the basics of Java, let’s consider a simple “Hello” program that provides an introductory understanding of the language. A detailed explanation of this example will be presented on the next page, offering a step-by-step guide for better comprehension.

class Sample{  

    public static void main(String args[]){  

     System.out.println("Hello Java");  



Leave a Comment