Introduction to Object-Oriented Programming in Java

Differences Between Object-Oriented and Procedural Programming

  • Procedural Programming focuses on the sequence of steps or procedures to solve a problem.

  • Advantages: Efficient for simple logic; lower initial development cost.

  • Disadvantages: High coupling between components makes maintenance difficult and limits extensibility.

  • Object-Oriented Programming (OOP) centers around objects—self-contained entities that encapsulate data and behavior.

  • Advantages: Low coupling, high reusability, and better scalability.

  • Disadvantages: Higher upfront design effort and abstract concepts that can complicate modeling.

Core Principles of OOP

  1. Encapsulation: Bundling data and methods that operate on that data within a single unit (class), and restricting direct access to internal state.
  2. Inheritance: Allowing one class to inherit properties and behaviors from another, promoting code reuse.
  3. Polymorphism: Enabling objects of different types to be treated as instances of a common superclass, supporting flexible and dynamic behavior.

OOP Development Phases

  • OOA (Object-Oriented Analysis): Understanding requirements by identifying real-world entities.
  • OOD (Object-Oriented Design): Structuring classes, relationships, and responsibilities.
  • OOP (Object-Oriented Programming): Implementing the design using a programming language like Java.

Classes and Objects

A class is an abstract blueprint that defines shared attributes and behaviors for a group of similar entities. It serves as a template for creating objects.

An object is a concrete instance of a class, representing a real-world entity with specific state and behavior.

Attributes and Methods

  • Attributes (fields) represent the state of an object (e.g., color for a Flower).
  • Methods define the actions an object can perform (e.g., eat() or run() for a Dog).

Clas Definition Syntax

[access modifier] class ClassName {
    // Fields (attributes)
    dataType fieldName;

    // Methods (behaviors)
    returnType methodName(parameters) {
        // method body
    }
}

Example: Defining a Student Class

public class Student {
    // Instance variables – each object has its own copy
    int studentId;
    String fullName;
    int yearsOld;
}

These fields are instance variables because they belong to individual objects. Memory for them is allocated only when an object is created using new.

Creating and Using Objects

In Java, the new operator allocates memory for an object on the heap. A reference variable holds the address of this object and resides on the stack.

Unlike C pointers, Java references cannot perform arithmetic or directly manipulate memory addresses—they provide safe, managed access to objects.

Accessing Object Members

  • Read: reference.fieldName
  • Write: reference.fieldName = value;

Example Usage

public class StudentDemo {
    public static void main(String[] args) {
        // Create a Student object
        Student learner = new Student();

        // Assign values to instance variables
        learner.studentId = 101;
        learner.fullName = "Alex";
        learner.yearsOld = 20;

        // Output object data
        System.out.println(learner.fullName + "'s ID: " + learner.studentId);
        System.out.println(learner.fullName + " is " + learner.yearsOld + " years old.");
    }
}

Note: Instance variables exist per object and are stored in heap memory. Local variables (like learner) are stored on the stack.

Key OOP Concepts in Java

  • Class vs. object distinction
  • Encapsulation through access modifiers
  • Constructors for object initialization
  • The this keyword for referencing currrent instance
  • The static keyword for class-level members
  • Inheritance and method overriding
  • The final keyword for immutability and inheritance control
  • Organizing code with package and import

Tags: java OOP encapsulation Inheritance Polymorphism

Posted on Wed, 12 Aug 2026 16:36:16 +0000 by novice@work