Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure and organize code. Java is a fully object-oriented programming language, and it supports the four main principles of OOP:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Below is a detailed explanation of these concepts and how they are implemented in Java.
1. Encapsulation
Encapsulation is the mechanism of wrapping data (variables) and code (methods) together as a single unit. It also restricts direct access to some of an object’s components, which is achieved using access modifiers.
Key Points:
- Data is hidden from outside the class using
private
access modifiers. - Access to the data is provided through getter and setter methods.
- Improves security and prevents unintended modification of data.
Example:
public class Person {
// Private variables (data hiding)
private String name;
private int age;
// Public getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) { // Validation
this.age = age;
}
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
person.setAge(25);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
2. Inheritance
Inheritance allows a class (subclass) to inherit properties and behaviors (methods and fields) from another class (superclass). It promotes code reusability and establishes a relationship between classes.
Key Points:
- The
extends
keyword is used to create a subclass. - A subclass can access non-private members of the superclass.
- Java supports single inheritance (a class can inherit from only one superclass).
Example:
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Subclass method
}
}
3. Polymorphism
Polymorphism allows objects to take on multiple forms. It enables a single method or class to have different implementations. There are two types of polymorphism in Java:
a. Compile-Time Polymorphism (Method Overloading)
- Multiple methods with the same name but different parameters.
- Example:
java class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
b. Runtime Polymorphism (Method Overriding)
- A subclass provides a specific implementation of a method that is already defined in its superclass.
- Example:
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); // Upcasting myDog.sound(); // Calls the overridden method in Dog class } }
4. Abstraction
Abstraction is the process of hiding implementation details and showing only the necessary features of an object. It is achieved using abstract classes and interfaces.
a. Abstract Classes
- A class declared with the
abstract
keyword. - Can have both abstract (no implementation) and concrete (with implementation) methods.
- Cannot be instantiated directly.
- Example:
abstract class Shape { abstract void draw(); // Abstract method void display() { // Concrete method System.out.println("Displaying shape"); } } class Circle extends Shape { @Override void draw() { System.out.println("Drawing a circle"); } } public class Main { public static void main(String[] args) { Shape shape = new Circle(); shape.draw(); shape.display(); } }
b. Interfaces
- A blueprint of a class that contains only abstract methods (before Java 8).
- From Java 8 onwards, interfaces can have default and static methods.
- A class implements an interface using the
implements
keyword. - Example:
interface Drawable { void draw(); // Abstract method default void display() { // Default method System.out.println("Displaying shape"); } } class Circle implements Drawable { @Override public void draw() { System.out.println("Drawing a circle"); } } public class Main { public static void main(String[] args) { Drawable circle = new Circle(); circle.draw(); circle.display(); } }
5. Key OOP Concepts in Java
Concept | Description |
---|---|
Encapsulation | Wrapping data and methods into a single unit and restricting access. |
Inheritance | A class inherits properties and behaviors from another class. |
Polymorphism | A single method or class can have multiple forms. |
Abstraction | Hiding implementation details and showing only essential features. |
Example Program Combining OOP Concepts
// Abstraction: Abstract class
abstract class Animal {
// Encapsulation: Private field
private String name;
// Constructor
public Animal(String name) {
this.name = name;
}
// Encapsulation: Getter
public String getName() {
return name;
}
// Abstraction: Abstract method
abstract void sound();
// Polymorphism: Method overloading
void eat() {
System.out.println(name + " is eating.");
}
void eat(String food) {
System.out.println(name + " is eating " + food);
}
}
// Inheritance: Subclass
class Dog extends Animal {
public Dog(String name) {
super(name);
}
// Polymorphism: Method overriding
@Override
void sound() {
System.out.println(getName() + " barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog("Buddy");
dog.sound(); // Polymorphism: Overridden method
dog.eat(); // Polymorphism: Overloaded method
dog.eat("bones"); // Polymorphism: Overloaded method
}
}
Summary
- Encapsulation: Protects data by restricting access and providing controlled methods.
- Inheritance: Promotes code reuse by allowing classes to inherit properties and behaviors.
- Polymorphism: Enables flexibility by allowing methods to behave differently based on the object.
- Abstraction: Simplifies complexity by hiding unnecessary details and exposing only essential features.
These OOP concepts are the foundation of Java programming and are essential for writing modular, reusable, and maintainable code.