Java OOP Concepts Cheat Sheet

Introduction

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to create models based on the real-world environment. Java, as an OOP language, facilitates four main concepts: Abstraction, Encapsulation, Inheritance, and Polymorphism. Additionally, advanced OOP concepts such as Composition, Aggregation, Association, Cohesion, Coupling, and Delegation play a significant role in designing robust software.

Java OOP Concepts Cheat Sheet

Concept Description
Object An instance of a class that contains attributes and methods.
Class A blueprint for creating objects, providing initial values for state (attributes) and implementations of behavior (methods).
Abstraction Hides complex implementation details and shows only the essential features of the object.
Encapsulation Bundles the data (variables) and methods that operate on the data into a single unit (class), and restricts access to some of the object's components.
Inheritance Allows a new class to inherit the properties and methods of an existing class.
Polymorphism Allows objects to be treated as instances of their parent class rather than their actual class. It comes in two types: compile-time (method overloading) and runtime (method overriding).
Composition A design principle where a class is composed of one or more objects of other classes, implying a strong relationship.
Aggregation A form of association with a weak relationship, where one class can reference another class.
Association Establishes a relationship between two classes through their objects.
Cohesion Measures how closely related and focused the responsibilities of a single class are.
Coupling Refers to how dependent classes are on one another. Lower coupling is preferred for better maintainability.
Delegation A design pattern where an object handles a request by delegating to a second helper object.

Explanation and Examples of Java OOP Concepts

Object

An object is an instance of a class containing attributes and methods. 

Example:

class Student {
    String name;
    int age;
    void study() {
        System.out.println(name + " is studying");
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.name = "John";
        student.age = 20;
        student.study(); // John is studying
    }
}

Explanation: student is an instance (object) of the Student class.

Class

A blueprint for creating objects, providing initial values for state (attributes) and implementations of behavior (methods). 

Example:

class Car {
    String model;
    int year;
    void drive() {
        System.out.println(model + " is driving");
    }
}

Explanation: Car class defines attributes model and year, and a method drive().

Abstraction

Hides complex implementation details and shows only the essential features of the object. 

Example:

abstract class Animal {
    abstract void makeSound();
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Bark");
    }
}

Explanation: The Animal class is abstract and hides the details of how makeSound is implemented.

Encapsulation

Bundles the data (variables) and methods that operate on the data into a single unit (class), and restricts access to some of the object's components. 

Example:

class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 0) {
            this.age = age;
        }
    }
}

Explanation: The Person class encapsulates the name and age fields, and restricts access to them through getters and setters.

Inheritance

Allows a new class to inherit the properties and methods of an existing class. 

Example:

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

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

Explanation: The Car class inherits the start() method from the Vehicle class.

Polymorphism

Allows objects to be treated as instances of their parent class rather than their actual class. It comes in two types: compile-time (method overloading) and runtime (method overriding). 

Example:

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

class Cat extends Animal {
    void makeSound() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Cat();
        myAnimal.makeSound(); // Meow
    }
}

Explanation: myAnimal is an instance of Cat but treated as an instance of Animal. The makeSound() method of Cat is called.

Composition

A design principle where a class is composed of one or more objects of other classes, implying a strong relationship. 

Example:

class Engine {
    void start() {
        System.out.println("Engine started");
    }
}

class Car {
    private final Engine engine = new Engine();

    void start() {
        engine.start();
        System.out.println("Car started");
    }
}

Explanation: The Car class is composed of an Engine object, indicating a strong relationship.

Aggregation

A form of association with a weak relationship, where one class can reference another class. 

Example:

class Department {
    String name;
    Department(String name) {
        this.name = name;
    }
}

class Student {
    String name;
    Department dept;

    Student(String name, Department dept) {
        this.name = name;
        this.dept = dept;
    }
}

Explanation: The Student class has a reference to a Department object, indicating a weak relationship.

Association

Establishes a relationship between two classes through their objects. 

Example:

class Library {
    String name;
    List<Book> books;
}

class Book {
    String title;
    String author;
}

Explanation: Library and Book have an association through a list of books.

Cohesion

Measures how closely related and focused the responsibilities of a single class are. 

Example:

class Printer {
    void printDocument(String document) {
        System.out.println(document);
    }
}

Explanation: The Printer class has high cohesion as it has a single, focused responsibility.

Coupling

Refers to how dependent classes are on one another. Lower coupling is preferred for better maintainability. 

Example:

class A {
    void doSomething() {
        System.out.println("A is doing something");
    }
}

class B {
    private A a = new A();
    void delegate() {
        a.doSomething();
    }
}

Explanation: Class B is tightly coupled with class A.

Delegation

A design pattern where an object handles a request by delegating to a second helper object. 

Example:

class Printer {
    void print(String text) {
        System.out.println(text);
    }
}

class PrinterService {
    private final Printer printer = new Printer();
    void print(String text) {
        printer.print(text);
    }
}

Explanation: PrinterService delegates the printing task to a Printer object.

Java OOP Concepts Cheat Sheet

Java OOP Concepts Cheat Sheet

Conclusion

Understanding and effectively implementing OOP concepts in Java is crucial for creating scalable, maintainable, and efficient software. This cheat sheet serves as a quick reference to the essential OOP principles, providing a foundation for robust Java development. Happy coding!

Comments