Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java programming idiom : Private implementation class

I found this construct in some code.

Is there any benefit to have a private static class implement A? This reminded me of the Pimpl idiom in C++. Is there any benefit to using the Pimpl idiom in Java?

public abstract class A {
    public void doStuff();

    public static A getNewInstance() {
       return new AImpl();
    }

    private static class AImpl extends A {
        public void doStuff()  {
           ....
        }    
    } 

}
like image 227
Chip Avatar asked Aug 10 '12 05:08

Chip


People also ask

What is private class in Java?

The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of the same package will not be able to access these members. Top-level classes or interfaces can not be declared as private because. private means “only visible within the enclosing class”.

What is implementing class in Java?

The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).

What is public class and private class in Java?

Public members can be accessed from the child class of the same package. Private members cannot be accessed from the child class of the same package. Public member can be accessed from non-child class of same package. Private members cannot be accessed from non-child class of same package.

Can We declare a class as private in Java?

Yes, we can declare a class as private but these classes can be only inner or nested classes. We can’t a top-level class as private because it would be completely useless as nothing would have access to it. can abstract class have final methods in java?

What is the use of implements in Java?

1 Definition and Usage. The implements keyword is used to implement an interface. The interface keyword is used to declare a special type of class that only contains abstract methods. 2 Multiple Interfaces 3 Related Pages. Read more about interfaces in our Java Interface Tutorial.

How to create a class in Java?

We can create a class in Java using the class keyword. For example, Here, fields (variables) and methods represent the state and behavior of the object respectively. In the above example, we have created a class named Bicycle.

What is the use of interface keyword in Java?

The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).


1 Answers

Is there any benefit to have a private static class implement A?

Well, it hides the implementation away completely, so from an encapsulation point of view it's quite nice. One situation I've seen this in a few times is custom comparators. For example:

public class Person
{
    public static final Comparator<Person> NAME_COMPARATOR = new NameComparator();
    public static final Comparator<Person> AGE_COMPARATOR = new AgeComparator();

    // Name, age etc properties

    private static class NameComparator implements Comparator<Person>
    {
        ...
    }

    private static class AgeComparator implements Comparator<Person>
    {
        ...
    }
}

There's no real need for the comparator implementation classes to be visible outside Person, and it's nice to be able to get an instance easily via the public static field.

No callers need to know the implementation - there could just be one comparator class which takes parameters, for example - they just express which comparator they want via the constants. (You could equally use an enum for this, of course.)

like image 172
Jon Skeet Avatar answered Oct 20 '22 19:10

Jon Skeet