Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code into reusable, self-contained objects. Java is an object-oriented language, and understanding OOP principles is fundamental to becoming a proficient Java developer. In this Core Java tutorial, we'll explore the core OOP principles in Java, along with detailed explanations and examples.
1. Classes and Objects:
Class: A class is a blueprint or template for creating objects. It defines the structure and behavior of objects.
Example:
public class Car {
String make;
String model;
int year;
void startEngine() {
System.out.println("Engine started.");
}
void stopEngine() {
System.out.println("Engine stopped.");
}
}
Object: An object is an instance of a class. It represents a real-world entity and has its own state and behavior.
Example:
Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2022;
myCar.startEngine();
2. Encapsulation:
Encapsulation is the practice of hiding the internal details of an object and providing a controlled interface for interacting with it. This is achieved through access modifiers like 'public', 'private', and 'protected'.
Example:
public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
In this example, the 'balance' variable is encapsulated by making it 'private', and access is provided through 'deposit' and 'getBalance' methods.
3.Inheritance:
Inheritance allows a class to inherit properties and behaviors from another class. In Java, you can create a new class based on an existing class (superclass) to reuse and extend its functionality.
Example:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
In this example, the 'Dog' class inherits the 'eat' method from the 'Animal' class and adds its own 'bark' method.
4.Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables flexibility in method invocation based on the actual object type.
Example:
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("Drawing a square.");
}
}
In this example, you can create objects of 'Circle' and 'Square' and invoke the 'draw' method on them, even though the method is originally defined in the 'Shape' class.
5. Abstraction:
Abstraction is the process of simplifying complex reality by modeling classes based on the essential properties and behaviors. Abstract classes and interfaces are used to achieve abstraction.
Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("Drawing a square.");
}
}
In this example, the 'Shape' class is abstract, and its 'draw' method is declared but not implemented. Subclasses like 'Circle' and 'Square' provide concrete implementations.
Conclusion:
Understanding and applying these core OOP principles in Java is essential for building maintainable, scalable, and organized code. They provide a solid foundation for designing software systems and enable code reuse, modularity, and extensibility. As you continue your Java journey, you'll find that OOP principles are at the heart of most Java applications and libraries.