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 { }
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
| Modifier | Allowed for Class Level |
| --------- | ----------------------- |
| private | β Not allowed |
| protected | β Not allowed |
| default | β
Allowed |
| public | β
Allowed |
π Access Modifiers Summary
| Modifier | Allowed for Class Level |
| --------- | ----------------------- |
| private | β Not allowed |
| protected | β Not allowed |
| default | β
Allowed |
| public | β
Allowed |
π¦ 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
Think of it like a company department structure:
| Department | Role |
| ----------------- | ----------------------- |
| Developer Package | Development classes |
| Tester Package | Testing related classes |
π Important Package Rule
π Inside the same package, class names must be unique.
Example:
developer package
Bala.java
Nantha.java
You cannot create another class with the same name:
developer package
Bala.java
Bala.java β
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 {
}
File name:
Sample.java
Compile command:
javac Sample.java
β Incorrect Example
Test.java
public class Sample { }
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();
}
}
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);
}
}
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();
}
}
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
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");
}
}
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)