Procedural programs, as in C, are organised around functions. As programs grow, the shared data that many functions change becomes hard to control. Object-oriented programming solves this by putting data and the functions that use it together.

Procedure-Oriented and Object-Oriented Programming

In procedure-oriented programming (POP), a program is a list of instructions grouped into functions, and data moves freely between functions, often as global variables. Any function can change the data, so errors spread, and it is hard to model real-world things. In object-oriented programming (OOP), a program is a collection of objects, each combining data and the methods that work on it. Data is hidden inside objects and can be reached only through their methods. OOP follows a bottom-up approach, building small objects and combining them.

Definition: Object-oriented programming. A style of programming that organises a program as a set of objects, each joining data with the methods that operate on it, and uses classes, encapsulation, inheritance and polymorphism.

The Basic Concepts

Objects are the basic run-time entities, such as a student, an account or a car. Each has state (data) and behaviour (methods). A class is a blueprint or template from which objects are made; Student is a class, and each student is an object, or instance, of it. Data abstraction shows only the essential features and hides the details, such as driving a car without knowing how the engine works. Encapsulation wraps data and methods into a single unit, the class, and hides the data from outside access, which is data hiding. Inheritance lets a new class acquire the properties of an existing class, such as Car inheriting from Vehicle, which supports reusability. Polymorphism means "many forms": the same operation behaves differently for different objects, such as a draw() method that draws a circle or a square. Dynamic binding means that the code to be run in response to a call is chosen at run time. Message passing means that objects communicate by calling each other's methods, as in account.deposit(500).

Benefits of OOP

OOP gives reusability through inheritance, which saves time; data security through encapsulation; easier maintenance and upgrades, since changes are local to a class; modelling of the real world; easier division of work among programmers; and scalability from small to large systems.

Applications of OOP

OOP is used in real-time systems, simulation and modelling, object-oriented databases, graphical user interfaces, games, CAD/CAM systems, AI and expert systems, and web and mobile applications.

Table 1.1: Procedure-oriented and object-oriented programming

Feature POP (C) OOP (Java)
Focus Functions Objects
Data Moves freely, often global Hidden in objects
Approach Top-down Bottom-up
Reuse Limited Inheritance
Example C Java, C++, Python

Example 1.1: Identifying OOP concepts

Problem: A bank program has a class Account with private balance and methods deposit() and withdraw(). SavingsAccount extends Account. Name the OOP concepts used.

Solution:

  1. Account is a class; each customer's account is an object.
  2. Keeping balance private and using deposit() and withdraw() is encapsulation and data hiding.
  3. SavingsAccount reusing Account is inheritance.
  4. If SavingsAccount defines its own withdraw() rule, that is polymorphism.

Result: Class, object, encapsulation, inheritance and polymorphism.

public class Account {
    private double balance;
    void deposit(double amt) { balance += amt; }
    double getBalance() { return balance; }
    public static void main(String[] args) {
        Account a = new Account();
        a.deposit(500);
        a.deposit(250);
        System.out.println("Balance = " + a.getBalance());
    }
}

Output

Balance = 750.0

Common mistakes

  • Treating a class and an object as the same. A class is the template; objects are instances.
  • Thinking that encapsulation means only putting code in a class. It also hides data.
  • Confusing abstraction (what is shown) with encapsulation (how it is hidden).

Key takeaways

  • OOP joins data and methods into objects.
  • A class is a blueprint; an object is an instance.
  • The pillars are abstraction, encapsulation, inheritance and polymorphism.
  • OOP gives reusability, security and easy maintenance.

Practice questions

  1. What is a class?
  2. Define encapsulation.
  3. What is polymorphism?

Answers to the practice questions

  1. A blueprint or template that defines the data and methods of its objects.
  2. The wrapping of data and methods into a single unit, the class, with the data hidden from outside access.
  3. The ability of the same operation to behave differently for different objects.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit I