DEV Community

Cover image for πŸš€ Day 15 of My Automation Journey – Packages, Access Modifiers & Method Overriding Basics
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 15 of My Automation Journey – Packages, Access Modifiers & Method Overriding Basics

In today’s learning session, I explored a few important Java concepts:

  • Access modifiers at class level
  • What is a Package
  • Rules for class names and file names
  • Accessing variables across packages
  • A quick look at Method Overriding limitations

These topics are very important when working with large Java applications and automation frameworks.

πŸ” Access Modifiers at Class Level

In Java, access modifiers control who can access a class, variable, or method.

However, an important rule many beginners miss is:

πŸ‘‰ Not all access modifiers are allowed at the class level.

❌ Not Allowed for Top-Level Classes

private class Bala { }
protected class Bala { }
Enter fullscreen mode Exit fullscreen mode

Java does not allow private or protected for top-level classes.

βœ… Allowed for Top-Level Classes

Only two modifiers are allowed:

public class Bala { }
class Bala { }  // default
Enter fullscreen mode Exit fullscreen mode
| Modifier  | Allowed for Class Level  |
| --------- | -----------------------  |
| private   | ❌ Not allowed           |
| protected | ❌ Not allowed           |
| default   | βœ… Allowed               |
| public    | βœ… Allowed               |
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Access Modifiers Summary

| Modifier  | Allowed for Class Level  |
| --------- | -----------------------  |
| private   | ❌ Not allowed           |
| protected | ❌ Not allowed           |
| default   | βœ… Allowed               |
| public    | βœ… Allowed               |
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ What is a Package in Java?

A Package in Java is simply a folder that organizes classes.
It helps developers manage large projects by grouping related classes together.

Example Structure
ITCompany (Project)

   developer  (Package)
        Bala.java
        Nantha.java

   tester (Package)
        Deepika.java
        Kumar.java
Enter fullscreen mode Exit fullscreen mode

Think of it like a company department structure:

| Department        | Role                    |
| ----------------- | ----------------------- |
| Developer Package | Development classes     |
| Tester Package    | Testing related classes |
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Important Package Rule

πŸ‘‰ Inside the same package, class names must be unique.

Example:

developer package

Bala.java
Nantha.java
Enter fullscreen mode Exit fullscreen mode

You cannot create another class with the same name:

developer package

Bala.java
Bala.java ❌
Enter fullscreen mode Exit fullscreen mode

This will cause a compiler error.

πŸ“ File Name vs Class Name Rule

In Java, when a class is public, the file name must match the class name.

Correct Example

public class Sample {
}
Enter fullscreen mode Exit fullscreen mode

File name:

Sample.java
Enter fullscreen mode Exit fullscreen mode

Compile command:

javac Sample.java
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect Example

Test.java
public class Sample { }
Enter fullscreen mode Exit fullscreen mode

This will cause a compiler error because the file name and class name differ.

πŸ‘¨β€πŸ’» Example: Developer Package

package developer;

class Bala {

    String work = "Development";
    protected String headphone = "Sony";
    public String designation = "Developer";

    Bala(int salary, String work) {
        this.work = work;
        System.out.println("This is Bala constructor");
    }

    private void details() {
        System.out.println(work);
    }

    public static void main(String[] args) {

        Bala bala1 = new Bala(1000, "ABC");
        bala1.details();

    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
| Variable | Access Modifier | Meaning |
| ----------- | --------------- | ------------------------------------- |
| work | default | Accessible within same package |
| headphone | protected | Accessible in same package + subclass |
| designation | public | Accessible everywhere |
| details() | private | Accessible only inside the class |

πŸ‘¨β€πŸ’» Example: Subclass in Same Package

package developer;

public class Nantha extends Bala {

    public Nantha(int i, String work) {
        super(i, work);
        System.out.println("This is Nantha constructor");
    }

    public static void main(String[] args) {

        Nantha nanthaKumar = new Nantha(1001, "AND");

        System.out.println(nanthaKumar.work);
        System.out.println(nanthaKumar.headphone);
        System.out.println(nanthaKumar.designation);

    }
}
Enter fullscreen mode Exit fullscreen mode

What Happens Here?

Because Nantha is in the same package, it can access:

Modifier Accessible?
default βœ… Yes
protected βœ… Yes
public βœ… Yes
private ❌ No

πŸ‘©β€πŸ’» Example: Different Package

package tester;

public class Deepika extends Bala {

    public static void play(){
        System.out.println("game 12");
    }

    public static void main(String[] args) {

        Deepika deepika = new Deepika();

    }
}
Enter fullscreen mode Exit fullscreen mode

If the class is in a different package, access becomes limited.

Modifier Accessible?
default βœ… Yes
protected βœ… Yes
public βœ… Yes
private ❌ No

⚠️ Important Note About Protected

protected works across packages only through inheritance.

Example:

tester.Deepika extends developer.Bala
Enter fullscreen mode Exit fullscreen mode

So Deepika can access protected members.

πŸ”„ Method Overriding (Basic Idea)

Method overriding happens when a child class provides its own implementation of a parent method.

Example:

Parent Class
class A {

    public static void play() {
        System.out.println("Game A");
    }

}
Child Class
class B extends A {

    public static void play() {
        System.out.println("Game B");
    }

}
Enter fullscreen mode Exit fullscreen mode

However, static methods are not truly overridden in Java.
They are method hidden, not overridden.

True overriding requires:

βœ” Same method name
βœ” Same parameters
βœ” Non-static method
βœ” Inheritance

🧠 Key Takeaways

βœ” private and protected cannot be used for top-level classes
βœ” Only public and default are allowed for class declaration
βœ” A package is like a folder that groups related classes
βœ” Class names must be unique within the same package
βœ” Public class name must match the file name
βœ” protected works across packages only via inheritance

🏁 Conclusion

Understanding packages and access modifiers is essential when building real-world Java applications.

  • These concepts help developers:
  • Organize large projects
  • Control access to variables and methods
  • Protect internal implementation
  • Build clean and maintainable code

For someone learning Java for Automation Testing, mastering these basics will make it much easier to understand frameworks like Selenium Page Object Model and Test Automation Architecture.

πŸ‘¨β€πŸ« Trainer: Nantha from Payilagam

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)