Spex3
    

Java Object-Oriented Programming (OOP) Fundamentals


    

    

Java Object-Oriented Programming (OOP) Fundamentals
Java is an object-oriented programming (OOP) language, meaning it focuses on creating objects that interact with each other.

1. Classes and Objects
1.1 What is a Class?
A class is a blueprint for creating objects. It defines attributes (variables) and behaviors (methods).

✅ Example of a Class in Java:


// Defining a class
class Car {
String brand; // Attribute
int speed; // Attribute

// Method to display car details
void showDetails() {
System.out.println("Brand: " + brand + ", Speed: " + speed);
}
}


1.2 What is an Object?
An object is an instance of a class. It has state (defined by attributes) and behavior (defined by methods).

✅ Example of Creating an Object:


public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.showDetails(); // Calling method
}
}


💡 Output:


Brand: Toyota, Speed: 120


2. Encapsulation
Encapsulation is hiding the internal details of an object and allowing controlled access through methods. This is achieved using private variables and public getter/setter methods.

✅ Example of Encapsulation:


class BankAccount {
private double balance; // Private variable

// Constructor
public BankAccount(double initialBalance) {
balance = initialBalance;
}

// Getter method
public double getBalance() {
return balance;
}

// Setter method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
account.deposit(500);
System.out.println("Balance: " + account.getBalance()); // Output: 1500.0
}
}


🔹 Benefits of Encapsulation:
✔ Protects data from unauthorized access.
✔ Provides controlled access using methods.

3. Inheritance
Inheritance allows a class (child) to inherit properties and behaviors from another class (parent).

3.1 Types of Inheritance in Java
✔ Single Inheritance: One child inherits from one parent.
✔ Multilevel Inheritance: Child inherits from a parent, which inherits from another parent.
✔ Hierarchical Inheritance: Multiple children inherit from the same parent.

✅ Example of Single Inheritance:


// Parent class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}

// Child class (inherits from Animal)
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited method
myDog.bark(); // Own method
}
}


💡 Output:


This animal eats food.
Dog barks.


4. Polymorphism
Polymorphism allows one interface to be used for different data types. It helps in code flexibility and reusability.

4.1 Method Overloading (Compile-time Polymorphism)
Method overloading allows multiple methods with the same name but different parameters.

✅ Example of Method Overloading:


class MathOperations {
// Method with two integers
int add(int a, int b) {
return a + b;
}

// Method with three integers
int add(int a, int b, int c) {
return a + b + c;
}
}

public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(5, 10)); // Calls method with 2 parameters
System.out.println(math.add(5, 10, 15)); // Calls method with 3 parameters
}
}


💡 Output:


15
30


4.2 Method Overriding (Runtime Polymorphism)
Method overriding allows a child class to provide a specific implementation of a method that is already defined in the parent class.

✅ Example of Method Overriding:


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

// Child class (overrides method)
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks.");
}
}

public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Polymorphic behavior
myAnimal.makeSound(); // Calls the overridden method
}
}


💡 Output:

Dog barks.

5. Abstraction
Abstraction hides implementation details and shows only essential features. This is done using abstract classes or interfaces.

5.1 Abstract Class
An abstract class contains abstract methods (without implementation) and regular methods.

✅ Example of an Abstract Class:


// Abstract class
abstract class Animal {
abstract void makeSound(); // Abstract method (no implementation)

void sleep() {
System.out.println("Sleeping...");
}
}

// Subclass must implement the abstract method
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks.");
}
}

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Calls the implemented method
myDog.sleep(); // Calls inherited method
}
}


💡 Output:


Dog barks.
Sleeping...

5.2 Interface (Full Abstraction)
An interface only contains method declarations (no implementation). Classes that implement an interface must provide an implementation.

✅ Example of an Interface:


// Interface
interface Animal {
void makeSound(); // Abstract method (no implementation)
}

// Class implementing the interface
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks.");
}
}

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Calls the implemented method
}
}


💡 Output:


Dog barks.

Key Differences: Abstract Class vs Interface

Feature Abstract Class Interface

Methods Can have both abstract & regular methods Only abstract methods (Java 8+ allows default methods)

Variables Can have instance variables Only constants (final variables)

Multiple Inheritance Cannot be inherited by multiple classes