OOP_in_Java
OOP_in_Java
Introduction:
Object-Oriented Programming (OOP) is a paradigm that uses “objects” to design
applications and programs. Java is one of the most popular OOP languages.
1. Encapsulation:
- Hiding internal state and requiring all interaction through methods.
- Achieved using private fields and public getter/setter methods.
2. Inheritance:
- Allows a new class to inherit properties and methods from an existing class.
- Promotes code reuse.
- Syntax: `class Subclass extends Superclass`
3. Polymorphism:
- Ability of different objects to respond differently to the same method call.
- Compile-time (method overloading) and runtime (method overriding) polymorphism.
4. Abstraction:
- Hiding complex implementation details and showing only necessary features.
- Achieved using abstract classes and interfaces.
Example:
```java
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
Conclusion:
OOP in Java enables modular, scalable, and maintainable code. Mastering the four
principles is key to effective Java programming.