Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Object-Oriented Programming in Java

Tech 1

Object-Oriented Concepts

Object-oriented programming (OOP) organizes software design around objects rather than actions. In Java, this approach models real-world entities with properties (attributes) and behaviors (methods).

Key Differences from Procedural Programming

  • Procedural: Focuses on step-by-step execution
  • Object-oriented: Emphasizes objects and their interactions

Example: Washing clothes

  • Procedural: Detailed manual steps
  • OOP: Using a washing machine object

Core OOP Features

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Classes and Objects

A class defines a blueprint for objects, specifying their attributes and methods. An object is a specific instance of a class.

Example class definition:

public class Animal {
    // Attributes
    String name;
    int age;
    
    // Behaviors
    public void eat() {
        System.out.println("Eating...");
    }
    
    public void sleep() {
        System.out.println("Sleeping...");
    }
}

Creating and Using Objects

public class Main {
    public static void main(String[] args) {
        Animal cat = new Animal();
        cat.name = "Whiskers";
        cat.age = 3;
        
        cat.eat();
        cat.sleep();
    }
}

Memory Management

  • Objects reside in heap memory
  • References to objects live in stack memory
  • Methods are stored once in class definition

Encapsulation

Encapsulation protects data by restricting direct access to class members. Use private variables with public getter/setter methods.

Example:

public class BankAccount {
    private double balance;
    
    public void setBalance(double amount) {
        if (amount > 0) {
            this.balance = amount;
        }
    }
    
    public double getBalance() {
        return balance;
    }
}

Constructors

Constructors initialize new objects. Java provides a default no-arg constructor if none is defined.

public class Book {
    private String title;
    private String author;
    
    // Default constructor
    public Book() {}
    
    // Parameterized constructor
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

JavaBean Standard

A proper JavaBean includes:

  1. Private feilds
  2. Public no-arg constructor
  3. Getter/setter methods
  4. Serializable implmeentation (optional)

Example:

public class Employee implements Serializable {
    private String id;
    private String department;
    
    public Employee() {}
    
    public Employee(String id, String dept) {
        this.id = id;
        this.department = dept;
    }
    
    // Getters and setters
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    // Additional methods...
}
Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.