1.What is the difference between a static method and an instance method in Java?
In Java, methods can be either static or instance:
- Static Method: Belongs to the class, not to any specific object. You can call it without creating an instance of the class.
- Instance Method: Belongs to an object of the class. You need to create an object to call it.
Example:
class Example {
static void staticMethod() {
System.out.println("Static method called");
}
void instanceMethod() {
System.out.println("Instance method called");
}
}
Usage:
Example.staticMethod(); // Valid, no object needed
new Example().instanceMethod(); // Valid, requires object
Key Differences:
Feature | Static Method | Instance Method |
---|---|---|
Belongs to | Class | Object |
Called using | Class name | Object reference |
Access to this |
❌ No | ✅ Yes |
Access to fields | Only static fields | Both static and instance fields |
2.Can a static method access instance variables in Java? Why or why not?
No, a static method cannot directly access instance variables or instance methods.
Why?
- Static methods belong to the class, not to any specific object.
- Instance variables belong to a specific object created from that class.
- Since static methods are called without an object, they don’t have access to instance-specific data (like instance variables or
this
keyword).
Example:
public class Test {
int instanceVar = 10;
static void staticMethod() {
// System.out.println(instanceVar); // ❌ Compile-time error
}
void instanceMethod() {
System.out.println(instanceVar); // ✅ OK
}
}
3.What is the difference between method overloading and method overriding in Java?
Feature | Method Overloading | Method Overriding |
---|---|---|
Definition | Defining multiple methods with same name but different parameter lists in the same class. | Redefining a method of a superclass in a subclass with same name and parameters. |
Purpose | Increases flexibility and readability (compile-time polymorphism). | Used to change/inherit behavior (runtime polymorphism). |
Class Relationship | Happens within the same class. | Happens between parent and child classes. |
Parameters | Must be different in number/type/order. | Must be exactly same as superclass method. |
Return Type | Can be same or different. | Must be same or covariant. |
Access Modifier | No restrictions. | Cannot reduce visibility. |
Polymorphism Type | Compile-time polymorphism | Runtime polymorphism |
Annotation | Not required. | Usually annotated with @Override |
🔸 Example of Overloading:
class MathUtil {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
🔸 Example of Overriding:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}
What is the difference between static binding and dynamic binding in Java method calls?
Java uses binding to link a method call with the method body. There are two types:
🔸 1. Static Binding (Early Binding):
-
Done at compile time.
-
Used for:
- Static methods
- Private methods
- Final methods
- Method overloading
✅ Example:
class Example {
static void show() {
System.out.println("Static method");
}
private void greet() {
System.out.println("Private method");
}
}
- The compiler knows exactly which method to call — no object type check needed at runtime.
🔸 2. Dynamic Binding (Late Binding):
-
Done at runtime.
-
Used for:
- Overridden instance methods
✅ Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Output: Dog barks (resolved at runtime)
}
}
- Even though
a
is declared asAnimal
, it refers to aDog
object — so theDog
's version is invoked.
📌 Key Differences:
Feature | Static Binding | Dynamic Binding |
---|---|---|
Time of resolution | Compile time | Runtime |
Applies to | Static, private, final methods | Overridden instance methods |
Based on | Reference type | Object type |
Polymorphism type | Compile-time polymorphism | Runtime polymorphism |
Can we override static methods in Java?
No, we cannot override static methods in Java. Instead, we can hide them — a concept known as method hiding.
Why?
- Overriding applies to instance methods — which are resolved at runtime based on the object's actual type.
- Static methods belong to the class, not to any instance. They are resolved at compile time using the reference type — not the object type.
🔸 Example:
class Parent {
static void greet() {
System.out.println("Hello from Parent");
}
}
class Child extends Parent {
static void greet() {
System.out.println("Hello from Child");
}
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.greet(); // Output: Hello from Parent (NOT Child)
}
}
Can static methods be abstract in Java?
No, static methods cannot be abstract in Java.
🔸 Why?
- An abstract method is meant to be overridden by subclasses — it has no body and must be implemented by a concrete subclass.
- A static method belongs to the class, not the object. Since static methods can’t be overridden (only hidden), making them abstract contradicts their purpose.
🔸 Compilation Error Example:
abstract class Example {
abstract static void greet(); // ❌ Compile-time error
}
💥 Error:
Illegal combination of modifiers: abstract and static
What is a covariant return type in method overriding in Java?
A covariant return type allows an overriding method to return a more specific (subclass) type than the method it overrides.
This feature was introduced in Java 5 to support better type safety and more expressive APIs during method overriding.
🔸 Example:
class Animal {}
class Dog extends Animal {}
class Parent {
Animal getAnimal() {
return new Animal();
}
}
class Child extends Parent {
@Override
Dog getAnimal() { // ✅ Covariant return type
return new Dog();
}
}
🔍 Here,
Dog
is a subclass ofAnimal
, so it's a valid override using a covariant return type.
🔹 Why Useful?
Covariant return types allow subclasses to provide more specific return values without requiring casting in the client code.
Before Java 5 (no covariance):
Dog d = (Dog) child.getAnimal(); // Must cast
With covariance:
Dog d = child.getAnimal(); // No cast needed ✅
⚠️ Rules to Remember:
- Only reference types can be covariant (e.g.,
String
,List
, custom classes). - Primitive types (like
int
,double
) cannot be covariant. - The covariant type must be a subclass of the original return type.
🔁 Follow-Up Questions:
-
What happens if a class has an abstract method but is not declared abstract?
-
Can we declare a constructor as abstract?
-
What happens if the return type is different in overriding?
-
What is covariant return type in method overriding?
-
Why can’t static methods use dynamic binding?
-
How does Java decide which method to call in case of overloading?
-
Can constructors participate in binding?
🔁 Follow-Up Questions:
- Can covariant return types apply to generics?
- What happens if we try to override a method with a primitive return type using a different primitive?
- Can we use covariant return types with interfaces?
Would you like to see an example with interfaces or generics next?
Would you like to explore one of these or continue to an advanced polymorphism question?