Java Object-Oriented Programming (OOP) Fundamentals
 Fundamentals.jpg)
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);
}
}
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
}
}
Brand: Toyota, Speed: 120
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
}
}
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
}
}
// 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
}
}
// 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
}
}
// 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
}
}