You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Marker interfaces are interfaces that don't have any fields, methods, or constants inside of it. They are also known as empty interfaces or tag interfaces. Examples of marker interfaceare Serializable, Cloneable and Remote interface. The purpose of marker interfaces are to provide run-time type information about an object to JVM and Compiler. They are mainly used in API development and in frameworks like Spring to provide additional information to the class.
495
+
Marker interfaces are interfaces that don't have any fields, methods, or constants inside of it. They are also known as **empty interfaces** or **tag interfaces**. Examples of marker interfaceare Serializable, Cloneable and Remote interface. The purpose of marker interfaces are to provide run-time type information about an object to JVM and Compiler. They are mainly used in API development and in frameworks like Spring to provide additional information to the class.
495
496
496
497
Some of the commonly used built-in marker interfaces are:
62. ### Can you override a private or static method?
4295
+
4296
+
**Short Answer:** No, you cannot override private or static methods in Java. These methods are not subject to polymorphism.
4297
+
4298
+
**Detailed Explanation:**
4299
+
4300
+
**1. Private Methods - Cannot Override:**
4301
+
4302
+
Private methods are not inherited by subclasses and therefore cannot be overridden. Since private members have class-level scope, they are not accessible in child classes.
4303
+
4304
+
```java
4305
+
class Parent {
4306
+
private void display() {
4307
+
System.out.println("Parent's private display");
4308
+
}
4309
+
}
4310
+
4311
+
class Child extends Parent {
4312
+
// This is NOT an override - it's a new method in Child class
Inthis example, `display()` in the Child classis a completely new method, not an override. This is called "method hiding" or "shadowing" at the compile level.
4330
+
4331
+
**Key Points about Private Methods:**
4332
+
- Private methods have class-level scope and are not visible outside the class
4333
+
- They cannot be inherited by subclasses
4334
+
- You can define a method with the same signature in the child class, but it's a new method, not an override
4335
+
- The @Override annotation would cause a compile error if used on a private method that shadows a parent's private method
Static methods belong to the class, not to instances. They cannot be overridden, but they can be "hidden" (redefined) in subclasses. When you call a static method on a reference, it's resolved at compile-time based on the reference type, not the runtime type.
4340
+
4341
+
```java
4342
+
classParent {
4343
+
staticvoidstaticDisplay() {
4344
+
System.out.println("Parent's static display");
4345
+
}
4346
+
}
4347
+
4348
+
classChildextendsParent {
4349
+
// This is method hiding, not overriding
4350
+
staticvoidstaticDisplay() {
4351
+
System.out.println("Child's static display");
4352
+
}
4353
+
}
4354
+
4355
+
publicclassTest {
4356
+
publicstaticvoidmain(String[] args) {
4357
+
Parent parent =newChild();
4358
+
parent.staticDisplay(); // Calls Parent's staticDisplay (resolved at compile-time)
Notice that even though `parent` refers to a `Child` object, calling `parent.staticDisplay()` invokes the Parent's version because static method resolution happens at compile-time based on the reference type.
4378
+
4379
+
**Key Points about Static Methods:**
4380
+
- Static methods are resolved at compile-time based on the reference type
4381
+
- Static methods are not polymorphic
4382
+
- When a child class has a static method with the same signature as the parent, it "hides" the parent's method
4383
+
-This is different from overriding because the determination of which method to call is made at compile-time, not runtime
4384
+
4385
+
**3.Comparison with Public/ProtectedInstanceMethods:**
4386
+
4387
+
```java
4388
+
classParent {
4389
+
publicvoidinstanceMethod() {
4390
+
System.out.println("Parent's instance method");
4391
+
}
4392
+
}
4393
+
4394
+
classChildextendsParent {
4395
+
@Override// This annotation works correctly
4396
+
publicvoidinstanceMethod() {
4397
+
System.out.println("Child's instance method");
4398
+
}
4399
+
}
4400
+
4401
+
publicclassTest {
4402
+
publicstaticvoidmain(String[] args) {
4403
+
Parent parent =newChild();
4404
+
parent.instanceMethod(); // Calls Child's instanceMethod (resolved at runtime)
4405
+
}
4406
+
}
4407
+
```
4408
+
4409
+
Output:
4410
+
```
4411
+
Child's instance method
4412
+
```
4413
+
4414
+
Here, the method is resolved at runtime based on the actual object type (Child), demonstrating true overriding with polymorphism.
4415
+
4416
+
**4. Why This Design?**
4417
+
4418
+
- **Private Methods:** Since they have class scope, inheritance doesn't apply. They're implementation details of a class.
4419
+
- **Static Methods:** Since they belong to the class (not to instances), they don't participate in polymorphic behavior, which is based on instance types.
4420
+
4421
+
**TableSummary:**
4422
+
4423
+
|MethodType|CanOverride?|ResolutionTime|Behavior|
4424
+
|---|---|---|---|
4425
+
|PublicInstance|Yes|Runtime (Polymorphic) |Method overriding with dynamic dispatch |
4426
+
|ProtectedInstance|Yes|Runtime (Polymorphic) |Method overriding with dynamic dispatch |
4427
+
|Private|No|Compile-time |Method hiding; new method in child class |
4428
+
| Static | No | Compile-time | Method hiding; resolved based on reference type |
0 commit comments