Object-Oriented Programming (OOP) Using Java
Introduction
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which contain data and methods that operate on that data. Java is a widely used OOP language that supports principles like encapsulation, inheritance, polymorphism, and abstraction.
1. Principles of OOP
1.1 Encapsulation
Encapsulation is the bundling of data (variables) and methods that operate on the data into a single unit, i.e., a class. Access to the data is restricted using access modifiers like private, protected, and public.
class Student {
private String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}1.2 Inheritance
Inheritance allows one class (child) to acquire the properties and behavior of another class (parent).
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}1.3 Polymorphism
Polymorphism allows methods to take multiple forms, such as method overloading and method overriding.
Method Overloading:
class MathOperations {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}Method Overriding:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}1.4 Abstraction
Abstraction hides implementation details and only exposes the necessary functionality.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with a key");
}
}2. Variables in Java
Variables store data in Java programs. They are categorized as:
2.1 Types of Variables
- Local Variables - Declared inside a method and accessible only within that method.
- Instance Variables - Declared inside a class but outside any method.
- Static Variables - Declared with the
statickeyword and shared among all objects of a class.
class Example {
int instanceVar = 10;
static int staticVar = 20;
void display() {
int localVar = 30;
System.out.println("Instance Variable: " + instanceVar);
System.out.println("Static Variable: " + staticVar);
System.out.println("Local Variable: " + localVar);
}
}3. Operators in Java
Java provides various operators for performing operations:
3.1 Arithmetic Operators
+, -, *, /, %
3.2 Relational Operators
==, !=, >, <, >=, <=
3.3 Logical Operators
&&, ||, !
3.4 Increment and Decrement Operators
Postfix Increment
int a = 5;
System.out.println(a++); // Prints 5, then increments a to 6Prefix Increment
int a = 5;
System.out.println(++a); // Increments a to 6, then prints 64. Java Control Statements
4.1 If-Else Statement
int age = 18;
if (age >= 18) {
System.out.println("Eligible to vote");
} else {
System.out.println("Not eligible to vote");
}4.2 Loops in Java
For Loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}While Loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}Do-While Loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);5. Exception Handling in Java
Exception handling allows the program to deal with runtime errors gracefully.
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}Conclusion
Object-Oriented Programming in Java provides a structured approach to software development by using objects and classes. It enables code reusability, scalability, and efficiency through core principles like encapsulation, inheritance, polymorphism, and abstraction. Additionally, Java provides control structures, variable types, operators, and decision-making statements to handle program flow effectively.