EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

OOPS in Java

Object-Oriented Programming (OOP) in Java refers to a programming approach where objects are used to represent and execute the code’s functionality. Objects are the core components, performing specific tasks as defined by the programmer.

OOP is designed to model real-world entities, such as inheritance, encapsulation, polymorphism, etc., in code. The primary objective is to bind data and the functions that operate on the data, ensuring that no other part of the program can directly access this data except through those functions.

Key OOP Concepts in Java

Java is built on the foundation of Object-Oriented Programming. Understanding key OOP concepts such as inheritance, encapsulation, abstraction, and polymorphism is critical to writing scalable and maintainable Java code.

Prerequisites

Before diving into the pillars of OOP, let’s review the essentials of method declaration and message passing in Java.

Method Declaration

A method in Java consists of six main components:

1. Access Modifier: Defines the accessibility of the method within the application. The common types are:

  • public: Accessible in all classes.
  • protected: Accessible within the package and in subclasses.
  • private: Accessible only within the class.
  • Default (no modifier): Accessible only within the same package.

2. Return Type: Specifies the data type returned by the method, or void if no value is returned.
3. Method Name: Follows the same naming conventions as fields, but typically starts with a lowercase letter.
4. Parameter List: The input parameters (if any), defined as a comma-separated list within parentheses.
5. Exception List: Specifies any exceptions that the method might throw.
6. Method Body: Contains the block of code that is executed when the method is called.

Message Passing

Objects in Java communicate with one another by sending messages, which involve calling methods. A message consists of the object’s name, the method to be executed, and any required information.

Java OOP Concepts

OOP in Java is centered around several key concepts:

1. Class: A class is a blueprint or prototype that defines the properties and behaviors common to all objects of a specific type. A class typically consists of fields (attributes) and methods (behaviors). In Java, a class can be defined as:

public class Employee {
    // Fields
    String name;
    int salary;

    // Methods
    void setDetails(String n, int s) {
        name = n;
        salary = s;
    }

    void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Salary: " + salary);
    }
}

2. Object: An object is an instance of a class. It represents a specific entity in a Java program, with its own state and behavior.

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setDetails("John Doe", 50000);
        emp.displayDetails();
    }
}

Output:

Name: John Doe
Salary: 50000

The Four Pillars of OOP

1. Abstraction: Abstraction involves hiding unnecessary details and showing only the essential features of an object. In Java, abstraction is achieved using abstract classes and interfaces.

Example of an abstract class:

abstract class Vehicle {
    abstract void start();
}

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

2. Encapsulation: Encapsulation is the practice of wrapping data and methods into a single unit (class). It restricts direct access to the data by making the fields private and providing public getter and setter methods to modify and access them.

class Person {
    private String name;

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

    public String getName() {
        return name;
    }
}

3. Inheritance: Inheritance allows a class to inherit properties and methods from another class. It promotes reusability and a hierarchical class structure. Inheritance is achieved using the extends keyword.

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

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

4. Polymorphism: Polymorphism allows methods or objects to behave in different ways based on the context. It is of two types: compile-time (method overloading) and runtime (method overriding).

Method Overloading (Compile-time Polymorphism):

class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }
}

Advantages of OOP over Procedural Programming

  • Code Reusability: By using classes and objects, you can reuse code efficiently without repetition.
  • Code Organization: Object-oriented code is better structured, making it easier to understand and maintain.
  • DRY Principle: OOP promotes the “Don’t Repeat Yourself” principle by allowing common code to be reused.
  • Faster Development: OOP speeds up development by encouraging modular components that can be reused across projects.
End of lesson.