Welcome to The Coding College! In this tutorial, we’ll explore Object-Oriented Programming (OOP) in Java, which is the foundation of modern software design. OOP helps developers create reusable, scalable, and maintainable applications.
What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects.” These objects encapsulate data (fields or attributes) and behavior (methods) to model real-world entities and interactions.
Core Principles of OOP
Java’s OOP is built on four key principles:
- Encapsulation
- Bundling data and methods into a single unit (class).
- Protects data by restricting direct access.
- Inheritance
- Enables a class to inherit properties and methods from another class.
- Promotes code reuse.
- Polymorphism
- Allows one interface to be used for different data types.
- Achieved through method overriding and overloading.
- Abstraction
- Hides implementation details and only exposes essential features.
- Achieved through abstract classes and interfaces.
Java OOP Terminologies
Term | Description |
---|---|
Class | Blueprint for creating objects. |
Object | Instance of a class containing attributes and methods. |
Constructor | Special method used to initialize objects. |
Method | Function defined inside a class to represent behavior. |
Field/Attribute | Variable defined in a class to represent object properties. |
Example: Basic OOP Structure
Here’s a simple example that demonstrates OOP concepts in Java:
// Class definition
class Car {
// Attributes
String brand;
String model;
int year;
// Constructor
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
// Method
public void displayInfo() {
System.out.println("Car: " + brand + " " + model + " (" + year + ")");
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Creating an object
Car car1 = new Car("Toyota", "Corolla", 2020);
car1.displayInfo();
}
}
Detailed Explanation
1. Class
A class is a template that defines the structure and behavior of an object.
class Car {
String brand;
String model;
int year;
}
2. Object
An object is an instance of a class, created using the new
keyword.
Car car1 = new Car("Toyota", "Corolla", 2020);
3. Constructor
A constructor initializes the object’s attributes when it is created.
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
4. Method
Methods define the behavior or actions of a class.
public void displayInfo() {
System.out.println("Car: " + brand + " " + model + " (" + year + ")");
}
OOP Features in Depth
1. Encapsulation
Encapsulation restricts direct access to fields and allows controlled access using methods.
class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. Inheritance
Inheritance allows a class to acquire properties of another class.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
3. Polymorphism
Polymorphism allows a method to have different implementations.
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
}
}
4. Abstraction
Abstraction hides complex details and shows only relevant information.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle.");
}
}
Benefits of Java OOP
- Modularity: Code is organized into classes and objects.
- Reusability: Inheritance and polymorphism allow code reuse.
- Scalability: Objects can be easily modified without affecting the entire application.
- Security: Encapsulation protects data integrity.
Practice Problems
- Create a
BankAccount
class with methods for deposit and withdrawal. - Build a class hierarchy for different types of vehicles.
- Implement a polymorphic example using animals and their unique sounds.
For more tutorials and hands-on exercises, visit The Coding College.