Method Overriding in Same & Different Packages in Java (With Simple Examples)

Method Overriding in Java in same and different packages with examples, rules, and access modifiers explained

If you're learning Java, you've probably come across the term method overriding. It’s one of those concepts that seems simple at first—but gets confusing when packages and access modifiers come into play.

Don’t worry. In this guide, we’ll break it down in a clear and practical way so you can actually understand and use it in exams and real programs.


What is Method Overriding?

Method overriding happens when a subclass provides its own version of a method that already exists in the parent class.

In simple words, the child class replaces the behavior of the parent class method.

class Parent {
    void show() {
        System.out.println("This is Parent class method");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("This is Child class method");
    }
}

When you run this, the child class method is executed instead of the parent one.

This is the core idea behind runtime polymorphism.


Why is Method Overriding Important?

You’ll use method overriding whenever you want to change or extend the behavior of an existing class.

  • It makes your code flexible
  • Helps in reusing existing code
  • Used heavily in real-world applications and frameworks

In short, it allows you to write smarter and cleaner programs.


Rules You Must Remember

Before jumping into packages, make sure you remember these basic rules:

  • Method name must be exactly the same
  • Parameters must match
  • Return type must be same (or compatible)
  • Access level cannot be more restrictive
  • Private methods cannot be overridden

Method Overriding in Same Package

When both classes are in the same package, overriding is straightforward.

You can override methods with:

  • default access
  • protected access
  • public access

Example (Same Package)

package mypack;

class Animal {
    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }

    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

Output:

Dog barks

Since both classes are in the same package, Java allows overriding without restrictions (except private methods).


Method Overriding in Different Packages

This is where most students get confused.

When classes are in different packages, access modifiers decide whether overriding is possible or not.

Key Idea

  • public → can be overridden anywhere
  • protected → can be overridden in different packages (with inheritance)
  • default → cannot be accessed outside package
  • private → not inherited, so no overriding

Example (Different Packages)

Step 1: Parent class

package pack1;

public class Animal {
    protected void sound() {
        System.out.println("Animal sound");
    }
}

Step 2: Child class

package pack2;
import pack1.Animal;

public class Dog extends Animal {
    public void sound() {
        System.out.println("Dog barks");
    }

    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

Output:

Dog barks

Here, the protected method is successfully overridden even though the classes are in different packages.


Access Modifier Summary Table

Modifier Same Package Different Package
private No No
default Yes No
protected Yes Yes
public Yes Yes

Method Overriding vs Overloading

These two are often confused, so here’s a quick comparison:

Feature Overriding Overloading
Where it happens Between parent & child Same class
Parameters Same Different
Execution Runtime Compile-time

Common Mistakes Students Make

  • Trying to override private methods
  • Changing method parameters
  • Reducing access level (e.g., public → private)
  • Forgetting to use @Override

A small mistake here can cause confusing errors, especially in exams.


Best Practices

  • Always use @Override annotation
  • Keep method names clear and meaningful
  • Use protected/public for better flexibility
  • Test your code with inheritance properly

Quick FAQ

Can we override methods in different packages?

Yes, but only if the method is public or protected.

Can private methods be overridden?

No, because they are not inherited.

Why is overriding used?

To change the behavior of a method in a subclass.


Conclusion

Method overriding is a powerful feature in Java that helps you customize inherited behavior. Once you understand how access modifiers work, especially across packages, the concept becomes much easier.

Practice with small programs like the ones above, and you’ll master it quickly.


📚 Read More

Comments