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() {
....
}
}
}
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”.
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 ).
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.
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?
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.
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.
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 ).
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With