In todayβs learning session, I explored some important core Java concepts that are very useful when building real-world applications and automation frameworks.
Todayβs topics included:
- π Access Modifiers
- π¦ Java Packages
- π File Name vs Class Name Rules
- π Basic Understanding of Method Overriding
These concepts are fundamental when writing clean, secure, and maintainable Java code.
π Access Modifiers in Java
Access modifiers control who can access a class, variable, or method.
Java provides four types of access modifiers:
| Access Modifier | Description |
| --------------- | ------------------------------------------------- |
| `private` | Accessible only within the same class |
| `default` | Accessible within the same package |
| `protected` | Accessible within the same package and subclasses |
| `public` | Accessible from anywhere |
π Access Modifiers vs Usage Level
Not all access modifiers are allowed at every level.
Access Modifiers for Classes
| Modifier | Top-Level Class Allowed? |
| --------- | ------------------------ |
| private | β Not allowed |
| protected | β Not allowed |
| default | β
Allowed |
| public | β
Allowed |
Example:
public class Bala { }
class Bala { } // default access
β Invalid:
private class Bala { }
protected class Bala { }
Java does not allow private or protected for top-level classes.
π Access Modifiers for Variables and Methods
Variables and methods can use all four access modifiers.
| Modifier | Same Class | Same Package | Subclass (Different Package) | Anywhere |
| --------- | ---------- | ------------ | ---------------------------- | -------- |
| private | β
| β | β | β |
| default | β
| β
| β | β |
| protected | β
| β
| β
| β |
| public | β
| β
| β
| β
|
π¦ What is a Package in Java?
A package in Java is a folder-like structure used to organize related classes.
It helps developers:
- Manage large projects
- Avoid class name conflicts
- Improve code readability
Example project structure:
ITCompany (Project)
developer (Package)
Bala.java
Nantha.java
tester (Package)
Deepika.java
Kumar.java
You can think of packages like departments in a company.
| Department | Responsibility |
| ----------------- | --------------------------- |
| developer package | Development related 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
β Not allowed:
developer package
Bala.java
Bala.java
This will cause a compiler error.
π File Name vs Class Name Rule
In Java, if a class is declared 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
File name:
Test.java
Code:
public class Sample {
}
β This will cause a compiler error because the file name and class name do not match.
π¨βπ» 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 + subclasses |
| designation | public | Accessible everywhere |
| details() | private | Accessible only inside this 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);
}
}
Access Result
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;
import developer.Bala;
public class Deepika extends Bala {
public static void play(){
System.out.println("Game 12");
}
public static void main(String[] args) {
Deepika deepika = new Deepika();
}
}
Access Result (Different Package)
| Modifier | Accessible? |
| --------- | ---------------------------- |
| default | β No |
| protected | β
Yes (only via inheritance) |
| public | β
Yes |
| private | β No |
Important Rule
protected members are accessible in another package only through inheritance.
Example:
tester.Deepika extends developer.Bala
π Method Overriding (Basic Idea)
Method overriding happens when a child class provides a new implementation of a method defined in the parent class.
Requirements for method overriding:
β Same method name
β Same parameters
β Inheritance relationship
β Method must not be static
β οΈ Static Methods and Overriding
Static methods cannot be truly overridden.
They are method hidden, not overridden.
Example:
class A {
public static void play() {
System.out.println("Game A");
}
}
class B extends A {
public static void play() {
System.out.println("Game B");
}
}
Here, play() in class B hides the method in class A.
This is called Method Hiding, not overriding.
π§ Key Takeaways
β private and protected cannot be used for top-level classes
β Only public and default are allowed for class declarations
β A package organizes related classes
β Class names must be unique within a package
β Public class name must match the file name
β protected works across packages only through inheritance
β Static methods cannot be overridden (method hiding)
π Conclusion
Understanding packages and access modifiers is essential when building large Java applications.
- These concepts help developers:
- Organize large projects
- Control access to data
- Protect internal implementation
- Write maintainable and scalable code
For someone learning Java for Automation Testing, mastering these fundamentals makes it easier to understand frameworks like:
- Selenium
- Page Object Model (POM)
- Test Automation Architecture
π€ A Small Note
I used AI assistance to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)