Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming approach based on classes and objects. Java is an object-oriented programming language that uses OOP concepts to make programs reusable, secure, flexible, and easy to maintain.
Main Concepts of OOP
1. Class
A class is a blueprint or template for creating objects. It contains variables and methods.
class Student { String name; void display() { System.out.println(name); } }
2. Object
An object is an instance of a class. It is used to access the properties and methods of a class.
Student s = new Student(); s.name = "Anuradha"; s.display();
3. Encapsulation
Encapsulation means wrapping data and methods into a single class. It also provides data security by restricting direct access using access modifiers such as private.
class Student { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
4. Inheritance
Inheritance allows a child class to acquire the properties and methods of a parent class. It is implemented using the extends keyword.
class Animal { void eat() { System.out.println("Eating"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } }
5. Polymorphism
Polymorphism means "many forms." It allows the same method name to perform different tasks.
There are two types:
- Compile-time polymorphism – Method Overloading
- Runtime polymorphism – Method Overriding
class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }
6. Abstraction
Abstraction means hiding unnecessary implementation details and showing only essential information. It can be achieved using abstract classes and interfaces.
abstract class Animal { abstract void sound(); }
Advantages of OOP
- Provides code reusability
- Improves data security
- Makes programs easier to maintain
- Supports modular programming
- Reduces code duplication
- Provides flexibility through polymorphism
- Makes large programs easier to manage
Summary
| Concept | Meaning |
|---|---|
| Class | Blueprint for objects |
| Object | Instance of a class |
| Encapsulation | Data hiding and wrapping |
| Inheritance | Reusing properties of a parent class |
| Polymorphism | Many forms |
| Abstraction | Hiding implementation details |
Conclusion: OOP is an important programming concept in Java that helps developers create reusable, secure, flexible, and maintainable software.