Object-Oriented Programming (OOP) Bsaics

Introduction
In the previous lesson, we explored Methods and Functions in Java, which are fundamental for code reusability and modularity. Building on that foundation, we now delve into Object-Oriented Programming (OOP), a core paradigm in Java that enhances modularity, data management, and scalability. This lesson covers the basics of OOP, including Classes and Objects, Types of Classes, Core OOP Concepts, and Encapsulation with Getters and Setters.
Classes and Objects
A class is a blueprint for creating objects, and an object is an instance of a class. Classes define the properties (fields) and behaviors (methods) of objects.
Example:
class Person {
String name;
int age;
void introduce() {
System.out.printf("Hi, my name is %s and I am %d years old.%n", name, age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.name = "Alice";
person.age = 25;
person.introduce();
}
}
/**
* Output:
* Hi, my name is Alice and I am 25 years old.
*/
Types of Classes in Java
Java supports different types of classes, each serving a unique purpose in programming.
Regular Classes (Concrete Classes)
These are standard classes used to define objects.
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
Abstract Classes
Abstract classes cannot be instantiated and may have abstract methods that subclasses must implement.
Example:
abstract class Vehicle {
String brand;
int speed;
Vehicle(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with a key");
}
}
Static Classes
Java does not support top-level static classes, but inner classes can be static.
Example:
class OuterClass {
static class StaticInnerClass {
void display() {
System.out.println("I am a static inner class");
}
}
}
Final Classes
A final class cannot be subclassed.
Example:
final class MathUtils {
static int square(int num) {
return num * num;
}
}
Core Concepts of OOP
Object-Oriented Programming consists of four core principles: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Encapsulation
Encapsulation restricts direct access to data and ensures controlled modification via methods.
Example:
class BankAccount {
private double balance;
// Constructor
BankAccount(double initialBalance) {
this.balance = initialBalance;
}
// Getter
public double getBalance() {
return balance;
}
// Setter
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
Inheritance
Inheritance allows a class to inherit properties and behavior from another class.
Example:
class Parent {
void show() {
System.out.println("I am a parent class");
}
}
class Child extends Parent {
void display() {
System.out.println("I am a child class");
}
}
Polymorphism
Polymorphism allows methods to take different forms, such as method overriding and overloading.
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
Abstraction
Abstraction hides implementation details and only shows essential features.
Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
Conclusion
Object-Oriented Programming (OOP) is the backbone of Java development. Understanding Classes and Objects, Types of Classes, and Core OOP Concepts (Encapsulation, Inheritance, Polymorphism, and Abstraction) is crucial for writing scalable and maintainable Java applications. Mastering these principles prepares you for advanced Java topics such as Design Patterns and Frameworks. Keep practicing, and happy coding!
About the author
I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.
Comments
Join the discussionNo comments yet. Be the first to comment.




