Skip to content

Commit a7446d0

Browse files
committed
Override private and static methods
1 parent 31328bd commit a7446d0

1 file changed

Lines changed: 140 additions & 1 deletion

File tree

README.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Frequently asked Java Interview questions
6868
| 59 | [What is the purpose of the this keyword?](#what-is-the-purpose-of-the-this-keyword) |
6969
| 60 | [What are generics in Java?](#what-are-generics-in-java) |
7070
| 61 | [What is type erasure in Java generics?](#what-is-type-erasure-in-java-generics) |
71+
| 62 | [Can you override a private or static method?](#can-you-override-a-private-or-static-method) |
7172
<!-- TOC_END -->
7273

7374
<!-- QUESTIONS_START -->
@@ -491,7 +492,7 @@ Frequently asked Java Interview questions
491492

492493
14. ### What are marker interfaces
493494

494-
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 interface are 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 interface are 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.
495496

496497
Some of the commonly used built-in marker interfaces are:
497498

@@ -4290,5 +4291,143 @@ Frequently asked Java Interview questions
42904291
42914292
**[⬆ Back to Top](#table-of-contents)**
42924293
4294+
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
4313+
public void display() {
4314+
System.out.println("Child's display");
4315+
}
4316+
}
4317+
4318+
public class Test {
4319+
public static void main(String[] args) {
4320+
Parent parent = new Child();
4321+
parent.display(); // Calls Parent's private display (not accessible here - compile error)
4322+
4323+
Child child = new Child();
4324+
child.display(); // Calls Child's display method
4325+
}
4326+
}
4327+
```
4328+
4329+
In this example, `display()` in the Child class is 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
4336+
4337+
**2. Static Methods - Cannot Override (Method Hiding Instead):**
4338+
4339+
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+
class Parent {
4343+
static void staticDisplay() {
4344+
System.out.println("Parent's static display");
4345+
}
4346+
}
4347+
4348+
class Child extends Parent {
4349+
// This is method hiding, not overriding
4350+
static void staticDisplay() {
4351+
System.out.println("Child's static display");
4352+
}
4353+
}
4354+
4355+
public class Test {
4356+
public static void main(String[] args) {
4357+
Parent parent = new Child();
4358+
parent.staticDisplay(); // Calls Parent's staticDisplay (resolved at compile-time)
4359+
4360+
Child child = new Child();
4361+
child.staticDisplay(); // Calls Child's staticDisplay
4362+
4363+
Parent.staticDisplay(); // Calls Parent's staticDisplay
4364+
Child.staticDisplay(); // Calls Child's staticDisplay
4365+
}
4366+
}
4367+
```
4368+
4369+
Output:
4370+
```
4371+
Parent's static display
4372+
Child's static display
4373+
Parent's static display
4374+
Child's static display
4375+
```
4376+
4377+
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/Protected Instance Methods:**
4386+
4387+
```java
4388+
class Parent {
4389+
public void instanceMethod() {
4390+
System.out.println("Parent's instance method");
4391+
}
4392+
}
4393+
4394+
class Child extends Parent {
4395+
@Override // This annotation works correctly
4396+
public void instanceMethod() {
4397+
System.out.println("Child's instance method");
4398+
}
4399+
}
4400+
4401+
public class Test {
4402+
public static void main(String[] args) {
4403+
Parent parent = new Child();
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+
**Table Summary:**
4422+
4423+
| Method Type | Can Override? | Resolution Time | Behavior |
4424+
|---|---|---|---|
4425+
| Public Instance | Yes | Runtime (Polymorphic) | Method overriding with dynamic dispatch |
4426+
| Protected Instance | 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 |
4429+
4430+
**[⬆ Back to Top](#table-of-contents)**
4431+
42934432

42944433
<!-- QUESTIONS_END -->

0 commit comments

Comments
 (0)