Skip to content

Commit 37b0de5

Browse files
authored
Update Java_7_Notes.md
1 parent ec0d98a commit 37b0de5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Java_7/Documents/Java_7_Notes.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,32 @@ obj.run();
130130
Output: Bike is running safely
131131
```
132132

133+
3. Static method hiding
134+
```java
135+
class Parent {
136+
// Static method in the parent class
137+
static void display() {
138+
System.out.println("Static method in Parent class");
139+
}
140+
}
141+
142+
class Child extends Parent {
143+
// Static method in the child class (hiding the parent method)
144+
static void display() {
145+
System.out.println("Static method in Child class");
146+
}
147+
}
148+
149+
public class MethodHidingExample {
150+
public static void main(String[] args) {
151+
Parent obj1 = new Parent();
152+
Parent obj2 = new Child();
153+
Child obj3 = new Child();
154+
155+
obj1.display(); // Calls Parent's display method
156+
obj2.display(); // Calls Parent's display method (reference type determines the method)
157+
obj3.display(); // Calls Child's display method
158+
}
159+
}
160+
161+
```

0 commit comments

Comments
 (0)