2

I'm studying inheritance in Java and the book I'm studying from uses an Employee class to explain several concepts. Since there can be only one (public) class in a java file of the same name, and this class creates objects of another class, I have to define an Employee class in the same file, without the public modifier. I was under the impression that classes defined this way after another class body in the same java file aren't visible to other classes in the same package. Here's a sample Java code for demonstration:

package book3_OOP;

public class TestEquality3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Employeee emp1 = new Employeee("John", "Doe");
        Employeee emp2 = new Employeee("John", "Doe");

        if (emp1.equals(emp2))
                System.out.println("These employees are the same.");
        else
            System.out.println("Employees are different.");


    }

}

class Employeee {
    private String firstName, lastName;

    public Employeee(String firstName, String lastName) {

        this.firstName = firstName;
        this.lastName = lastName;

    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public boolean equals(Object obj) {

        //object must equal itself
        if (this == obj)
            return true;

        //no object equals null
        if (obj == null)
            return false;

        //test that object is of same type as this
        if(this.getClass() != obj.getClass())
            return false;

        //cast obj to employee then compare the fields
        Employeee emp = (Employeee) obj;
        return (this.firstName.equals (emp.getFirstName())) && (this.lastName.equals(emp.getLastName()));

    }
}

For instance, the class Employeee is visible to all classes in the package book3_OOP. Which is the reason behind the extra 'e' in Employee. As of now I have about 6 employee classes in this package, such as Employee5, Employee6, and so on.

How do I ensure that the second class defined in this way in a .java file aren't exposed to other classes in the same package? Using other modifiers like private or protected throw errors.

Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
  • 7
    Make Employee a private inner class. – PM 77-1 Mar 29 '15 at 02:39
  • Read [this](https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) for more information about *inner classes* (like PM 77-1 suggested). – Tom Mar 29 '15 at 02:41

1 Answers1

5

Make Employee a static nested class of TestEquality3:

public class TestEquality3 {
    public static void main(String[] args) {
        Employee emp = new Employee("John", "Doe");
    }

    private static class Employee {
        private String firstName;
        private String lastName;

        public Employee(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
}

You should also do this to your other Employee classes. If there is any conflict with another class, you can use the enclosing class name to disambiguate:

TestEquality3.Employee emp = new TestEquality3.Employee("John", "Doe");
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
  • Oh I see. I'm not aware of this construct. How does `private` and `static` make the necessary difference in this case? I mean, you use `static` when you need to refer to a field/method without having to instantiate a class first. So, what's the theory behind adding `private` and `static` to the employee class here? – Manish Giri Mar 29 '15 at 03:02
  • The difference is, that his class is *inside* another one. Your classes are not. – Tom Mar 29 '15 at 03:05
  • 1
    @Manish As Tom said, the main difference is that `Employee` now is a nested class of `TestEquality3`. I made it private so it can't be accessed from outside the enclosing class, but this is optional. Making it static means you don't need an instance of `TestEquality3` to use it. Check this [other answer](http://stackoverflow.com/questions/29260297/constructor-inside-inner-static-class-in-java/29260653#29260653) for more details. – Anderson Vieira Mar 29 '15 at 03:08
  • Ah that makes perfect sense now. Thank You! I wonder why the book didn't use this construct. – Manish Giri Mar 29 '15 at 03:11