Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private class as return type from public method

Tags:

Why is this valid?

Foo.java

public class Foo {      public Bar getBar() {         return new Bar();     }      private class Bar {}  } 

If Bar is private, how will users of this class use this method? Polymorphism can be used of course, but shouldn't this be invalid and the declaration should indicate this as returning an Object?

like image 562
Noel De Martin Avatar asked Dec 16 '13 12:12

Noel De Martin


People also ask

Can you declare a private class in a namespace Java?

No, Allowing classes to be private to a namespace would achieve no meaningful level of protection. Because private means that the member is only access in the containing class. Since a top-level class has no class containing it; it cannot be private or protected.

Can we call a private method inside a public method?

An object user can use the public methods, but can't directly access private instance variables. You can make methods private too. Object users can't use private methods directly. The main reason to do this is to have internal methods that make a job easier.

What method returns a private variable?

the getter methods getAge() and getName() returns the value of private variables.

Can a private method can be called from outside the class it is defined in?

You can only use private methods with: This means you can't call private methods from outside the class that defines them. Because that would require an “explicit receiver”.


1 Answers

I've just been doing a bit of research on this and have not been able to find a definitive answer. It seems most likely that it is just an oversight on the part of the Java language designers and since it doesn't actually do any harm it has been left. It's no different really from putting a public method into a private class. Nothing stops you doing this, even though there is no way to actually access that public method.

Certainly NetBeans gives you the warning "Exporting non-public type through public API" when you try to do this. I expect most other environments will give a similar warning.

The returned object is entirely useless to anyone who tries to use it (unless they use reflection), pretty much all they can do is store it into an Object (or any other super class that they do have access to) and then pass that Object around.

You could potentially want to do this if the passed Object is being used as a "handle" that gets passed around but never operated on. In that case though it would still make much more sense to have the class public but make all the methods within it private to prevent them being acted on outside your class (or define a public interface to return and have the private class implement that).

So the answer seems to be:

It probably shouldn't be valid, but as it doesn't do any harm it has never been blocked.

There is a good answer here on a similar subject:

Exporting non-public type through public API

like image 128
Tim B Avatar answered Nov 12 '22 18:11

Tim B