Commit 407d87a
committed
feat: add example demonstrating @deprecated and @SuppressWarnings annotations
WHAT was added:
- Created `OldClass` with:
- `display()` method (normal, not deprecated).
- `show()` method marked with `@Deprecated` to indicate it should not be used in new code.
- Created `Annotations` class with a `main` method:
- Calls `show()` while suppressing compiler warnings using `@SuppressWarnings("deprecation")`.
KEY LEARNINGS:
1. @deprecated:
- Marks a method, class, or field as obsolete or unsafe to use.
- Compiler generates a warning when the deprecated element is used.
- Often used when APIs are replaced by newer versions but kept for backward compatibility.
2. @SuppressWarnings("deprecation"):
- Suppresses warnings triggered by using deprecated APIs.
- Should be used sparingly and only when you understand the implications.
- Example: maintaining legacy code while avoiding unnecessary compiler noise.
3. Best Practices:
- Prefer new methods over deprecated ones whenever possible.
- Document why suppression is necessary if you use `@SuppressWarnings`.
REAL-WORLD APPLICATIONS:
- ✅ Migrating legacy code: `@Deprecated` helps signal outdated APIs.
- ✅ Backward compatibility: Older methods remain available but marked as discouraged.
- ✅ Clean build pipelines: `@SuppressWarnings` avoids unnecessary warnings in stable legacy code.
- ✅ API evolution: Libraries like Spring, Java SDK, Android SDK use `@Deprecated` to phase out old methods gracefully.
RULE OF THUMB:
- Use `@Deprecated` to communicate API evolution to developers.
- Use `@SuppressWarnings` only when absolutely necessary, with clear documentation.
- Always migrate to newer APIs in the long run for better performance and maintainability.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 1a0e8e3 commit 407d87a
1 file changed
+22
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
0 commit comments