6.2 Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a powerful paradigm that helps developers create modular, maintainable, and flexible software. Java supports OOP through its core principles: Encapsulation, Abstraction, Inheritance, and Polymorphism. Understanding these concepts is crucial to writing well-organized code.
- The Four Principles of Object-Oriented Programming (OOP)
- Abstracting Concrete Ideas to Create Classes and Objects
- Encapsulating Data to Control the State of Your Object
- Modeling Object Behavior with Interfaces
- Live Demo: Using Encapsulation to Validate Object State
Learning Objectives:
- Understand the core principles of OOP: Encapsulation, Abstraction, Inheritance, and Polymorphism.
- Learn how to design a class with fields, methods, and constructors.
- Explore how inheritance allows for code reuse and hierarchy in class design.
- Understand how interfaces promote abstraction and flexibility.
The Four Principles of Object-Oriented Programming
1. Encapsulation
Encapsulation refers to controlling access to an object’s data by bundling the data (fields) and the methods that modify the data (setters) within a class. It protects the internal state of an object and ensures controlled access through getter and setter methods.
2. Abstraction
Abstraction focuses on simplifying complex systems by modeling real-world entities and behaviors into classes and objects. In Java, abstraction is implemented using abstract classes and interfaces, which define the structure without providing concrete details.
3. Inheritance
Inheritance allows one class to acquire properties and behaviors of another class. This supports code reuse and hierarchical class relationships, where a subclass inherits from a parent class but can also have its own unique methods and fields.
4. Polymorphism
Polymorphism means "many forms," and it allows objects to be treated as instances of their parent class. This concept allows for flexibility in handling objects of different types through a common interface or base class.
2. Abstracting Concrete Ideas to Create Classes and Objects
In Java, abstraction helps hide unnecessary details while focusing on essential characteristics. Abstract classes and interfaces enable developers to define the structure of objects without specifying the exact implementation.
Example:
abstract class Vehicle {
abstract void startEngine();
}
class Car extends Vehicle {
@Override
void startEngine() {
System.out.println("Car engine starts");
}
}
In this example, Vehicle is an abstract class, meaning it provides no direct implementation of startEngine(). The Car class extends Vehicle and provides a concrete implementation.
3. Encapsulating Data to Control the State of Your Object
Encapsulation involves keeping an object's data private and allowing controlled access via public methods. This principle ensures that objects maintain control over their internal state and only valid data is assigned.
Example:
public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Here, the balance field is private, meaning it can only be modified by the deposit() method. This restricts unwanted access and enforces valid state changes.
4. Modeling Object Behavior with Interfaces
Interfaces allow different classes to implement the same behavior without being related through inheritance. Interfaces in Java define a set of methods that implementing classes must provide, encouraging abstraction and flexibility.
Example:
interface Drawable {
void draw();
}
class Circle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a rectangle");
}
}
In this example, both Circle and Rectangle implement the Drawable interface. They provide their own version of the draw() method, promoting flexibility and loose coupling.
5. Live Demo: Using Encapsulation to Validate Object State
Encapsulation can help ensure that objects maintain valid states. In this demo, we will create a Person class where the age is controlled through encapsulation to prevent invalid values from being set.
Example:
public class Person {
private int age;
public void setAge(int age) {
if (age > 0 && age < 120) {
this.age = age;
} else {
System.out.println("Invalid age");
}
}
public int getAge() {
return age;
}
}
In this example, the age field is encapsulated within the Person class, ensuring that the age value remains within a valid range. This approach ensures the integrity of the object’s state.
Conclusion
The four principles of Object-Oriented Programming—Encapsulation, Abstraction, Inheritance, and Polymorphism—are fundamental to writing modular, maintainable, and flexible code. By mastering these principles, you can build robust applications in Java that are easier to understand and extend.
Java 8 offers powerful features such as abstract classes, interfaces, and encapsulation methods that allow you to leverage OOP concepts effectively, enabling you to model real-world systems more intuitively.