UNIT 2 – Object-Oriented Programming in Java Notes

New Delhi, Education Beat: In the ever-evolving world of programming, Java has carved out a reputation as one of the most structured and disciplined languages. At the heart of its design lies Object-Oriented Programming (OOP)—a paradigm that mirrors real-world entities through classes and objects. Unit 2 of Java programming takes students deeper into this paradigm, exploring class creation, constructors, method overloading, the powerful this keyword, static members, and access specifiers. These concepts not only define Java but also establish its edge as a language for building robust, modular, and secure software.

Download UNIT 2 – Object-Oriented Programming in Java Notes

Get simplified revision notes for this unit:
Download Unit 2 Notes PDF

Building Blocks: Classes and Objects

The Essence of a Class

In Java, a class acts as a blueprint or template from which objects are created. It encapsulates data (variables) and behavior (methods) into one logical unit. For instance:

class Car {
String brand;
int speed;

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

Here, Car is the class, holding the attributes brand and speed and the method drive().

Objects: Bringing Classes to Life

An object is an instance of a class. Using the new keyword, programmers can create multiple objects from a single class, each holding unique data.

Car c1 = new Car();
Car c2 = new Car();

Objects give meaning to code by allowing programmers to represent entities—be it a bank account, a student, or an online order—in a structured manner.

Constructors: The First Step in Object Setup

A constructor in Java is a special method used to initialize objects. Unlike regular methods, it has the same name as the class and no return type.

Default Constructor

If no constructor is explicitly defined, Java provides a default constructor that sets initial values.

class Student {
Student() {
System.out.println("Student object created");
}
}

Parameterized Constructor

For more control, programmers can define parameterized constructors, which accept arguments to set object properties at creation.

class Student {
String name;
int age;

Student(String n, int a) {
name = n;
age = a;
}
}

This flexibility ensures that every object begins life in a well-defined state.

The Role of the this Keyword

In Java, the this keyword holds significant power. It refers to the current object and resolves ambiguities, especially when instance variables share names with parameters.

class Employee {
String name;

Employee(String name) {
this.name = name; // distinguishes instance variable from parameter
}
}

Beyond that, this can be used to invoke current object methods or even call another constructor within the same class (this()). It is a small keyword with a big impact in keeping code readable and error-free.

Method Overloading: Same Name, Different Roles

Real-world actions often come in variations, and Java handles this with method overloading. In this concept, multiple methods share the same name but differ in parameter type, number, or order.

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

This feature allows intuitive code design, making methods adaptable to different data types without requiring multiple confusing names.

Static Members: Shared Across Objects

In many cases, certain data or methods need to be shared by all objects of a class rather than being unique to each. Java provides this through static variables and static methods.

Static Variables

A static variable belongs to the class, not the object. For instance, a schoolName field in a Student class can be shared by all student objects.

Static Methods

Static methods, like main(), can be called without creating objects. They are widely used for utility functions and shared behavior.

This mechanism reduces redundancy and ensures efficient memory use.

Access Specifiers: Encapsulation in Action

One of Java’s greatest strengths is encapsulation, the practice of hiding implementation details and exposing only necessary functionality. Access specifiers enforce this principle by controlling visibility.

  • public: Accessible from anywhere.

  • private: Accessible only within the same class.

  • protected: Accessible within the package and subclasses.

  • default (no modifier): Accessible only within the package.

By carefully applying these specifiers, programmers ensure modular, secure code where internal data is shielded from unauthorized manipulation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top