Skip to content

Commit 407d87a

Browse files
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

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Section21AnnotationsandJavaDoc/src/Annotations.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,25 @@ public static void main(String[] args) {
1616
o.show(); //Deprecated Method
1717
}
1818
}
19+
20+
/*
21+
1. @Deprecated
22+
- Jab kisi method/class ko future me use na karne ki recommendation hoti hai
23+
to usse @Deprecated mark karte hain.
24+
- Compiler warning deta hai ki ye method "old" hai aur avoid karna chahiye.
25+
- Example: o.show() ek deprecated method hai.
26+
27+
2. @SuppressWarnings("deprecation")
28+
- Jab tum intentionally deprecated method ko use karna chahte ho
29+
aur warning avoid karni hai, to ye annotation use karte ho.
30+
- Sirf warning suppress hoti hai, code chalna continue karega.
31+
32+
3. Is example me:
33+
- `OldClass` ke andar `show()` ko deprecated mark kiya gaya.
34+
- `main()` me use call kiya gaya, but warning suppress karne ke liye
35+
@SuppressWarnings("deprecation") lagaya gaya.
36+
37+
4. Use Case:
38+
- Jab purane APIs ya libraries use kar rahe ho lekin abhi migrate nahi kar pa rahe,
39+
to warnings avoid karne ke liye ye annotation lagate ho.
40+
*/

0 commit comments

Comments
 (0)