Skip to content

Commit 1a0e8e3

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 6a5f4df commit 1a0e8e3

File tree

1 file changed

+5
-35
lines changed

1 file changed

+5
-35
lines changed

Section21AnnotationsandJavaDoc/src/Annotations.java

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
1-
2-
/*abstract class Parent1{
3-
abstract public void display();
4-
}
5-
6-
class Child extends Parent1
7-
{
8-
public void display() //Abstract method overriding a method.
9-
{
10-
11-
}
12-
}*/
13-
14-
/*class Parent{
15-
public void display(){}
16-
}
17-
class Child extends Parent
18-
{
19-
//To avoid this type of silly Mistake use Annotations.
20-
@Override
21-
public void display(){
22-
23-
}
24-
}*/
25-
26-
27-
28-
class OldClass
29-
{
30-
public void display()
31-
{
1+
class OldClass {
2+
public void display() {
323
System.out.println("Hello");
334
}
5+
346
@Deprecated
35-
public void show()
36-
{
7+
public void show() {
378
System.out.println("Hi");
389
}
3910
}
4011

41-
public class Annotations
42-
{
12+
public class Annotations {
4313
@SuppressWarnings("deprecation")
4414
public static void main(String[] args) {
4515
OldClass o=new OldClass();

0 commit comments

Comments
 (0)