Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects and classes. An object combines data and the operations that work on that data. OOP helps developers create modular, reusable, maintainable, and scalable software.

What is OOP?

OOP is a programming approach in which real-world entities or concepts are modeled as objects. An object contains data (state) and methods (behavior).

Key Concepts in OOP

  • Class: A blueprint or template used to create objects.
  • Object: An instance of a class.
  • Method: A function or behavior defined inside a class.
  • Attribute / Field: Data associated with an object or class.
  • Inheritance: A mechanism through which a class can acquire properties and behavior from another class.
  • Polymorphism: The ability of the same interface, method, or operation to behave differently depending on the object or context.
  • Encapsulation: Bundling data and the methods that operate on it and controlling access to the data.
  • Abstraction: Hiding unnecessary implementation details and exposing essential features.

Four Pillars of Object-Oriented Programming

The four commonly recognized fundamental principles of OOP are Encapsulation, Inheritance, Polymorphism, and Abstraction.

1. Encapsulation

Encapsulation combines data and the methods that operate on that data into a single unit and controls how the data is accessed.

2. Inheritance

Inheritance allows a new class to acquire or reuse properties and behavior of an existing class.

3. Polymorphism

Polymorphism allows the same operation or interface to produce different behavior depending on the object or context.

4. Abstraction

Abstraction hides unnecessary implementation details and exposes only the essential functionality.

Exam Tip: Remember the four pillars of OOP: Encapsulation, Inheritance, Polymorphism, and Abstraction.

Class and Object

Classes and objects are fundamental concepts of object-oriented programming.

Class

A class is a blueprint or template that defines the data and behavior that objects of that type can have.

// Java class example class Student { // Fields private String name; private int age; private double marks; // Method public void setDetails( String n, int a, double m) { name = n; age = a; marks = m; } public void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Marks: " + marks); } }

Object

An object is an instance of a class. An object has state, behavior, and identity.

// Creating and using an object Student s1 = new Student(); s1.setDetails( "John", 20, 85.5); s1.display();
Class Object
Blueprint or template Instance of a class
Defines data and behavior Has actual state and behavior
Can be used to create objects Created from a class
One class can create many objects Each object has its own identity

Encapsulation

Encapsulation is the practice of bundling data and the methods that operate on that data within a class and controlling access to the internal state.

How to Achieve Encapsulation

  • Declare fields with restricted access, commonly private.
  • Provide public methods when controlled access is required.
  • Validate data before changing the object's state.
// Encapsulation example in Java class BankAccount { // Private fields private String accountNumber; private double balance; // Getter public String getAccountNumber() { return accountNumber; } // Getter public double getBalance() { return balance; } // Setter public void setAccountNumber(String accNo) { accountNumber = accNo; } // Controlled modification public void deposit(double amount) { if (amount > 0) { balance += amount; } } }

Benefits of Encapsulation

  • Data Protection: Prevents direct uncontrolled access to internal data.
  • Validation: Allows validation before modifying data.
  • Maintainability: Internal implementation can change without changing how the class is used.
  • Controlled Access: Provides read-only, write-only, or controlled access where appropriate.

Inheritance

Inheritance is a mechanism through which one class derives from another class and can reuse or extend its accessible members.

Important Terms

  • Superclass / Base Class: The class being inherited from.
  • Subclass / Derived Class: The class that inherits from the superclass.

Types of Inheritance

  • Single Inheritance: One subclass inherits from one superclass.
  • Multilevel Inheritance: A class inherits from a class that itself inherits from another class.
  • Hierarchical Inheritance: Multiple subclasses inherit from the same superclass.
  • Multiple Inheritance: A class inherits from multiple parent classes. Java does not support this through classes.
  • Hybrid Inheritance: A combination of different inheritance types.
// Single inheritance in Java class Animal { void eat() { System.out.println("Eating..."); } void sleep() { System.out.println("Sleeping..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } // Using inheritance Dog d = new Dog(); d.eat(); d.sleep(); d.bark();

Benefits of Inheritance

  • Code Reusability: Existing functionality can be reused.
  • Extensibility: A subclass can add new functionality.
  • Method Overriding: Supports runtime polymorphism.
  • Hierarchical Classification: Related classes can be organized into a hierarchy.
Note: Java does not support multiple inheritance of classes. However, a Java class can implement multiple interfaces.

Polymorphism

Polymorphism means "many forms". It allows the same method name, interface, or operation to represent different behavior depending on the context.

Types of Polymorphism in Java

1. Compile-Time Polymorphism

In Java, compile-time polymorphism is commonly achieved through method overloading.

Method overloading occurs when methods have the same name but different parameter lists.

// Method overloading class Calculator { int add( int a, int b) { return a + b; } int add( int a, int b, int c) { return a + b + c; } double add( double a, double b) { return a + b; } }

2. Runtime Polymorphism

Runtime polymorphism is commonly achieved through method overriding, where a subclass provides its own implementation of an inherited method.

// Method overriding class Animal { void makeSound() { System.out.println( "Animal makes a sound"); } } class Dog extends Animal { void makeSound() { System.out.println( "Dog barks"); } } class Cat extends Animal { void makeSound() { System.out.println( "Cat meows"); } } // Runtime polymorphism Animal myAnimal; myAnimal = new Dog(); myAnimal.makeSound(); // Output: Dog barks myAnimal = new Cat(); myAnimal.makeSound(); // Output: Cat meows
Exam Tip:
Overloading → Compile-time polymorphism
Overriding → Runtime polymorphism

Abstraction

Abstraction means hiding unnecessary implementation details and exposing only the essential functionality required by the user.

Ways to Achieve Abstraction in Java

  • Abstract Classes: Classes declared using the abstract keyword.
  • Interfaces: Define a contract that implementing classes must follow.

Abstract Classes

// Abstract class example abstract class Shape { abstract void draw(); void display() { System.out.println( "This is a shape"); } } class Circle extends Shape { void draw() { System.out.println( "Drawing Circle"); } }

Interfaces

// Interface example interface Vehicle { void start(); void stop(); } class Car implements Vehicle { public void start() { System.out.println( "Car started"); } public void stop() { System.out.println( "Car stopped"); } }
Abstract Class Interface
Can contain abstract and concrete methods Can contain abstract methods and, in modern Java, default, static and private methods
Can have instance variables Fields are implicitly public, static and final
Can have constructors Cannot have constructors
A class can extend only one class A class can implement multiple interfaces
Exam Note: In older Java terminology, interface methods were commonly described as abstract by default. Since Java 8, interfaces can also contain default and static methods, and since Java 9 they can contain private methods.

Constructors

A constructor is a special member of a class that is invoked when an object is created. It is commonly used to initialize an object's state.

Types of Constructors

  • No-Argument Constructor: A constructor that accepts no arguments.
  • Parameterized Constructor: A constructor that accepts one or more parameters.
  • Copy Constructor: A programmer-defined constructor that initializes an object using another object of the same class.
// Constructor examples in Java class Student { String name; int age; // No-argument constructor Student() { name = "Unknown"; age = 0; } // Parameterized constructor Student( String n, int a) { name = n; age = a; } // Programmer-defined copy constructor Student( Student s) { name = s.name; age = s.age; } void display() { System.out.println( "Name: " + name + ", Age: " + age); } } // Creating objects Student s1 = new Student(); Student s2 = new Student( "John", 20); Student s3 = new Student(s2);

Constructor Characteristics

  • Has the same name as the class.
  • Has no return type, not even void.
  • Is invoked when an object is created.
  • Can be overloaded.
  • If a class declares no constructor, Java provides a compiler-generated default constructor.
Important: A default constructor specifically means the no-argument constructor supplied by the compiler when no constructor is declared. A programmer-written no-argument constructor is not technically a compiler-provided default constructor.

Access Modifiers in Java

Access modifiers control the visibility and accessibility of classes, fields, methods, and constructors.

Types of Access Levels

Modifier Same Class Same Package Subclass Other Package
private Yes No No No
default (package-private) Yes Yes Only if in same package No
protected Yes Yes Yes* Yes*, through inheritance
public Yes Yes Yes Yes
Protected Access: A protected member can be accessed by subclasses in another package, but the access is subject to Java's protected-access rules.
// Access modifiers example class AccessExample { private int privateVar; int defaultVar; protected int protectedVar; public int publicVar; private void privateMethod() { } void defaultMethod() { } protected void protectedMethod() { } public void publicMethod() { } }
Exam Tip: For basic Java access-control questions, remember: private → default → protected → public, from most restrictive to most accessible.

Static Keyword

In Java, the static keyword indicates that a member belongs to the class rather than to individual objects. Static members are associated with the class itself.

Static Variables

  • Also called class variables.
  • Shared by objects of the same class.
  • Only one class-level variable is maintained for the class.

Static Methods

  • Belong to the class rather than individual objects.
  • Can be called using the class name.
  • Cannot directly access instance variables or instance methods.
  • Cannot directly use this or super.
// Static keyword example class Counter { static int count = 0; Counter() { count++; } static void displayCount() { System.out.println( "Count: " + count); } } // Creating objects Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = new Counter(); Counter. displayCount(); // Output: Count: 3
Important: The main() method in a standard Java application is declared static so that the JVM can invoke it without first creating an instance of the class.

OOP vs Procedural Programming

Procedural programming organizes programs primarily around procedures or functions, whereas OOP organizes programs around objects and classes.

Aspect Procedural Programming Object-Oriented Programming
Primary Organization Functions / procedures Classes and objects
Approach Often follows a top-down design approach Often associated with bottom-up design
Data and Behavior Often handled separately Often bundled within objects
Data Hiding Generally less central to the paradigm Encapsulation provides controlled access
Reusability Can use functions and other techniques Uses classes, composition, inheritance, etc.
Polymorphism Not a defining feature Important OOP feature
Examples C, Pascal Java, C++, C#

Benefits of Object-Oriented Programming

OOP provides several mechanisms that can make large software systems easier to design, develop, test, and maintain.

Main Benefits of OOP

  • Modularity: Programs can be organized into classes and objects.
  • Reusability: Existing classes and components can be reused.
  • Maintainability: Well-designed classes can make changes easier to manage.
  • Scalability: Applications can be extended using new classes and components.
  • Data Protection: Encapsulation allows controlled access to internal state.
  • Flexibility: Polymorphism allows different implementations to be used through a common interface.
  • Real-world Modeling: Real-world entities can be represented using objects.
  • Team Development: Different developers can work on separate classes or components.

Real-world Applications of OOP

  • Banking Systems: Account, Customer, Transaction
  • E-commerce: Product, ShoppingCart, Customer, Order
  • Game Development: Player, Enemy, Weapon
  • GUI Applications: Button, Window, Menu
  • Enterprise Applications: Customer, Employee, Invoice, Order
Exam Tip: Be prepared to explain the four pillars of OOP with examples. Questions on class vs object, overloading vs overriding, inheritance, encapsulation, and abstract class vs interface are especially important.

Quick Revision: OOP

Concept Meaning Key Point
Class Blueprint for objects Defines data and behavior
Object Instance of a class Has state, behavior and identity
Encapsulation Bundling and controlled access Protects internal state
Inheritance Acquiring/reusing class functionality Promotes reuse and specialization
Polymorphism Many forms Overloading and overriding in Java
Abstraction Hiding implementation details Abstract classes and interfaces
Constructor Initializes objects No return type
Static Belongs to the class Shared class-level member