Object-Oriented Programming – Basics and Concepts


Table of Contents

  1. Introduction to Object-Oriented Programming (OOP)
  2. Core Principles of OOP
  3. Classes and Objects
  4. Constructors
  5. The this Keyword
  6. Encapsulation
  7. Inheritance
  8. Polymorphism
  9. Abstraction
  10. Summary and Best Practices

1. Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. Java is a fully object-oriented language (with minor exceptions like primitive types) and leverages OOP principles to create reusable, scalable, and maintainable applications.

OOP enables you to model real-world systems more naturally, where everything is treated as an object with properties (fields) and behaviors (methods).


2. Core Principles of OOP

There are four fundamental principles of OOP:

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

These principles work together to provide a flexible and powerful design structure.


3. Classes and Objects

Class

A class is a blueprint for creating objects. It defines the properties and behaviors of the objects.

class Car {
String brand;
int speed;

void drive() {
System.out.println("Driving at " + speed + " km/h");
}
}

Object

An object is an instance of a class.

public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.drive(); // Outputs: Driving at 120 km/h
}
}

Each object created from the same class has its own set of variables and can perform methods defined in the class.


4. Constructors

A constructor is a special method that is called when an object is instantiated. Its name is the same as the class name and it has no return type.

Example:

class Car {
String brand;

Car(String brandName) {
brand = brandName;
}
}

You can also have default constructors (no parameters) or parameterized constructors.

Java supports constructor overloading, where multiple constructors with different parameter lists are defined in the same class.


5. The this Keyword

The this keyword is a reference to the current object. It is used to distinguish class fields from parameters with the same name.

class Car {
String brand;

Car(String brand) {
this.brand = brand; // Refers to class field, not constructor parameter
}
}

It can also be used to call other constructors within the same class using this() and to pass the current object as an argument.


6. Encapsulation

Encapsulation is the process of bundling data (fields) and methods that operate on that data into a single unit, i.e., a class. It also involves restricting direct access to some of an object’s components, which is usually done using access modifiers like private, public, and protected.

Example:

class Account {
private double balance;

public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public double getBalance() {
return balance;
}
}

Here, the balance field is private and only accessible through public methods, enforcing controlled access.


7. Inheritance

Inheritance allows a class to inherit fields and methods from another class. The class that inherits is called the subclass, and the class being inherited from is the superclass.

Example:

class Vehicle {
void start() {
System.out.println("Vehicle starting...");
}
}

class Car extends Vehicle {
void honk() {
System.out.println("Car honking...");
}
}

Now Car has both start() and honk() methods.

Java supports single inheritance but not multiple inheritance through classes (it supports it via interfaces).


8. Polymorphism

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class.

Compile-Time Polymorphism (Method Overloading)

We already saw this in the Methods module—same method name, different parameters.

Run-Time Polymorphism (Method Overriding)

Occurs when a subclass provides a specific implementation of a method already defined in its superclass.

class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Animal obj = new Dog();
obj.sound(); // Outputs: Dog barks

The method that gets called is determined at runtime, not compile time.


9. Abstraction

Abstraction is the process of hiding the internal details and showing only the essential features.

Java supports abstraction through:

  • Abstract classes – cannot be instantiated and can contain abstract methods
  • Interfaces – fully abstract types in earlier versions of Java, but since Java 8, interfaces can have default and static methods too

Abstract class example:

abstract class Shape {
abstract void draw();
}

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

You cannot create an object of Shape, but you can of Circle, which implements the abstract method.


10. Summary and Best Practices

In this module, we explored:

  • The foundation of Object-Oriented Programming in Java
  • How classes and objects model real-world entities
  • The use of constructors and this keyword
  • Encapsulation for data hiding
  • Inheritance for code reusability
  • Polymorphism for flexible method behavior
  • Abstraction for hiding implementation complexity

Best Practices:

  • Favor encapsulation to protect internal state.
  • Use inheritance judiciously; prefer composition when inheritance isn’t appropriate.
  • Override methods responsibly and always use @Override.
  • Make classes and methods only as accessible as necessary (private by default).
  • Use interfaces to define contracts for unrelated classes.