Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Object-Oriented Programming – Part 1

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects, rather than functions or logic. Java is a purely object-oriented language (except for primitive types) and heavily relies on OOP concepts such as classes, objects, constructors, encapsulation, and access control. Understanding these concepts is essential for building real-world, scalable, and maintainable Java applications.


1. Classes and Objects

Class

A class is a blueprint or template used to create objects. It defines properties (variables) and behaviors (methods) that an object will have.

Key Points

  • Logical entity
  • Does not occupy memory
  • Defines structure and behavior

Syntax

class ClassName {
    // variables
    // methods
}

Object

An object is an instance of a class. It represents a real-world entity and occupies memory.

Key Points

  • Physical entity
  • Created using new keyword
  • Can access class members

Example: Class and Object

class Student {
    String name;
    int age;

    void display() {
        System.out.println(name + " " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Rahul";
        s1.age = 20;
        s1.display();
    }
}

Explanation

Student is a class, and s1 is an object of that class.


2. Constructors in Java

What is a Constructor?

A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type.

Key Characteristics

  • Automatically called when object is created
  • Used to initialize instance variables
  • Can be overloaded

2.1 Default Constructor

A constructor with no parameters is called a default constructor.

class Demo {
    Demo() {
        System.out.println("Default constructor called");
    }
}

2.2 Parameterized Constructor

A constructor that accepts parameters is called a parameterized constructor.

class Employee {
    int id;
    String name;

    Employee(int i, String n) {
        id = i;
        name = n;
    }
}

2.3 Copy Constructor

A copy constructor creates a new object by copying values from another object.

class Book {
    String title;

    Book(Book b) {
        title = b.title;
    }
}

3. this Keyword

Description

The this keyword refers to the current object of the class. It is mainly used to differentiate between instance variables and parameters with the same name.

Uses of this

  • Refer current class variables
  • Call current class methods
  • Call current class constructor

Example

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

Explanation

this.name refers to the instance variable, while name refers to the constructor parameter.


4. Access Modifiers in Java

Description

Access modifiers control the visibility and accessibility of classes, variables, methods, and constructors.


Types of Access Modifiers

ModifierAccessibility
publicAccessible everywhere
privateAccessible only within the same class
protectedAccessible within same package or subclass
defaultAccessible within same package

Example

class Test {
    public int a = 10;
    private int b = 20;
}

5. Encapsulation and Data Hiding

Encapsulation

Encapsulation is the process of wrapping data and methods together into a single unit (class).

Data Hiding

Data hiding is achieved by declaring variables as private and restricting direct access from outside the class.

Benefits

  • Improves security
  • Protects data from misuse
  • Makes code maintainable
  • Improves flexibility

Example

class Account {
    private double balance;

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

    public double getBalance() {
        return balance;
    }
}

6. Getters and Setters

Description

Getters and setters are public methods used to access and modify private variables safely.

  • Getter → returns value
  • Setter → sets value

Example

class Student {
    private int marks;

    public void setMarks(int m) {
        if (m >= 0 && m <= 100) {
            marks = m;
        }
    }

    public int getMarks() {
        return marks;
    }
}

Explanation

Direct access to marks is restricted. Validation is applied using setter.

Example:

java

public class Student {
    // Private fields (encapsulation)
    private String name;
    private int rollNumber;
    private double marks;
    
    // Default constructor
    public Student() {
        this.name = "Unknown";
        this.rollNumber = 0;
        this.marks = 0.0;
    }
    
    // Parameterized constructor
    public Student(String name, int rollNumber, double marks) {
        this.name = name;
        this.rollNumber = rollNumber;
        this.marks = marks;
    }
    
    // Copy constructor
    public Student(Student other) {
        this.name = other.name;
        this.rollNumber = other.rollNumber;
        this.marks = other.marks;
    }
    
    // Getters
    public String getName() {
        return name;
    }
    
    public int getRollNumber() {
        return rollNumber;
    }
    
    // Setters
    public void setName(String name) {
        this.name = name;
    }
    
    public void setMarks(double marks) {
        if (marks >= 0 && marks <= 100) {
            this.marks = marks;
        }
    }
    
    // Method
    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Roll Number: " + rollNumber);
        System.out.println("Marks: " + marks);
    }
    
    public static void main(String[] args) {
        Student s1 = new Student("John", 101, 85.5);
        s1.display();
        
        Student s2 = new Student(s1);
        s2.setName("Jane");
        s2.display();
    }
}

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥