Object-Oriented Programming (OOP) helps in code reusability, maintainability, and organization using objects and classes.
- Java
- C++
- Python
- C#
- Ruby
Feature | Interface | Abstract Class |
---|---|---|
Methods | Only method signatures (no implementation, except default methods in Java 8+) | Can have both abstract methods and concrete methods |
Inheritance | Used for defining behavior across unrelated classes | Used for shared behavior in a class hierarchy |
- Used to compare object equality and enable correct hashing.
- Override when creating custom objects that need to be compared logically.
- Occurs in multiple inheritance when a class inherits from two interfaces with the same method.
- Fix: Use default methods in interfaces or avoid multiple inheritance of implementations.
- Frees memory by removing unused objects to prevent memory leaks.
- Runs automatically but can be triggered manually with
System.gc()
.
- Used for class-level variables, methods, blocks, and nested classes.
- Belongs to the class, not instances.
- Definition: An immutable object cannot be modified after creation.
- Where to Use: String, Wrapper classes, and custom immutable objects.
- Why: Ensures thread safety and efficient caching.
- How: Use
final
fields and avoid setters.
Feature | Composition | Aggregation |
---|---|---|
Association | Strong (contained object cannot exist without the container) | Weak (contained object can exist independently) |
- Cohesion: How well a class’s methods relate to its purpose.
- Coupling: Dependency between classes.
Feature | Heap | Stack |
---|---|---|
Stores | Objects | Method calls and local variables |
Scope | Shared among threads | Thread-specific |
Management | Managed by Garbage Collector | Auto-cleared |
- Definition: An event disrupting program execution.
- Types:
- Checked: Must be handled (e.g.,
IOException
). - Unchecked: Runtime errors (e.g.,
NullPointerException
).
- Checked: Must be handled (e.g.,
- Simple, readable, maintainable, and follows best practices.
- Occurs when a subclass defines a static method with the same signature as a static method in the parent class.
- Unlike method overriding, the method call depends on the reference type.
Feature | Abstraction | Polymorphism |
---|---|---|
Definition | Hides implementation details using abstract classes or interfaces | Allows methods to behave differently based on the object type |
Example | Abstract classes, Interfaces | Method overriding, Method overloading |