Oop Introduction

Oop Introduction

A school program needs reliable data and clear rules. Object-oriented programming groups related state and behavior. A Student object can hold a name and provide a report method; it is one instance of a class definition.

  1. Understand

    Object-oriented programming groups related state and behavior.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    An object model is a design choice, not a claim that every Java value is an object. Primitive marks remain primitive values.

Oop Introduction

Object-Oriented Programming (OOP) is not just a way of writing code—it's a philosophy. It’s how we model the real world inside our programs. Java is one of the most popular languages to embrace OOP from the ground up. If you're new to Java, understanding its object-oriented foundation is your first real step into writing clean, scalable, and maintainable software.

What is Object-Oriented Programming?

OOP is a programming paradigm centered around the concept of objects—instances of classes—that interact with one another. Instead of thinking in terms of functions and logic alone, OOP encourages thinking in terms of entities, their attributes, and their behaviors.

Why Java Uses OOP

Java uses OOP to:

  • Promote code reusability via inheritance
  • Enhance security using encapsulation
  • Enable flexibility through polymorphism
  • Reduce complexity with abstraction

Core Concepts of Java OOP

Let’s dive into the four foundational pillars of Java OOP:

1. Class and Object

A class is like a blueprint. An object is a real-world instance of that blueprint.

class Car {
    String color;
    void drive() {
        System.out.println("The car is driving.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();       // object creation
        myCar.color = "Red";
        myCar.drive();               // method call
        System.out.println(myCar.color);
    }
}
The car is driving.
Red

Explanation:

Here, Car is the class with a property and a method. myCar is an object created from that class. It accesses the method and variable defined in the class.

2. Encapsulation

Encapsulation means bundling data and methods that work on that data into a single unit (class), and restricting access to some components.

class BankAccount {
    private double balance = 1000.0;

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        System.out.println("Initial Balance: " + account.getBalance());
        account.deposit(500);
        System.out.println("Updated Balance: " + account.getBalance());
    }
}
Initial Balance: 1000.0
Updated Balance: 1500.0

Explanation:

The balance variable is private, meaning it cannot be accessed directly. This ensures data protection. Access is controlled using public methods.

3. Inheritance

Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse and logical hierarchy.

class Animal {
    void makeSound() {
        System.out.println("Some generic sound");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.makeSound(); // inherited method
        d.bark();      // specific method
    }
}
Some generic sound
Dog barks

Explanation:

The Dog class extends Animal. It gains access to the makeSound() method and also adds its own behavior with bark().

4. Polymorphism

Polymorphism allows us to perform a single action in different ways. Java supports two types:

  • Compile-time polymorphism (method overloading)
  • Runtime polymorphism (method overriding)
class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}
    
public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(5, 3));        // int version
        System.out.println(calc.add(5.2, 3.8));    // double version
    }
}
8
9.0

Explanation:

The method add is overloaded with different parameter types. This is polymorphism in action—same name, different behavior.

5. Abstraction

Abstraction hides unnecessary details and shows only the essential features of an object.

abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Circle();  // abstract class reference
        s.draw();
    }
}
Drawing a circle

Explanation:

The abstract class Shape defines an abstract method draw(). The Circle class implements that method, completing the abstraction cycle.

Final Thoughts

Java OOP is the backbone of writing meaningful Java applications. As you move forward, remember: classes are blueprints, objects are real, and encapsulation, inheritance, polymorphism, and abstraction are the four wheels that keep your code vehicle moving smoothly.

Important boundaries and mistakes

An object model is a design choice, not a claim that every Java value is an object. Primitive marks remain primitive values.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: An object model is a design choice, not a claim that every Java value is an object.

Java 17 · Main.java · complete practice program

public class Main  {
  interface Named  {
    String label();
  }
  static class Student implements Named  {
    static String school = "River School";
    String name;
    Student(String name)  {
      this.name = name;
    }
    public String label()  {
      return name;
    }
  }
  public static void main(String[] args)  {
    Student s = new Student("Diya");
    System.out.println(s.name);
  }
}

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  interface Named  {
    String label();
  }
  static class Student implements Named  {
    static String school = "River School";
    String name;
    Student(String name)  {
      this.name = name;
    }
    public String label()  {
      return name;
    }
  }
  public static void main(String[] args)  {
    Student s = new Student("Diya");
    System.out.println(s.name);
  }
}

Expected output

Diya

Object-oriented programming groups related state and behavior. A Student object can hold a name and provide a report method; it is one instance of a class definition.

An object model is a design choice, not a claim that every Java value is an object. Primitive marks remain primitive values.

Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

Diya

Object-oriented programming groups related state and behavior. A Student object can hold a name and provide a report method; it is one instance of a class definition. An object model is a design choice, not a claim that every Java value is an object. Primitive marks remain primitive values.

What to remember

Object-oriented programming groups related state and behavior. A Student object can hold a name and provide a report method; it is one instance of a class definition. An object model is a design choice, not a claim that every Java value is an object. Primitive marks remain primitive values.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Class →