File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes public methods in default class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public methods in default class" Watch "public methods in default class" New topic
Author

public methods in default class

midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Hi guys!

A public class can be accessed by everything. But what about the public methods in default classes? Is there a special purpose of public modifier for methods in default class?
Got this doubt because i noticed that main method will not work without public keyword even if in default class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Public methods in non-public classes are quite common. They're used when we want to provide an implementation of public methods, but not make the implementing class available to the world.

For example, java.util.AbstractList defines a private class Itr implements Iterator, which is what we get when we call the iterator() method on ArrayList or LinkedList. The outside world can't reference this class, and doesn't need to know anything about it, other than that there's something there that implements Iterator.

Another example is if you define a non-public class and override toString() in that class. When you call System.out.println(instance_of_my_non_public_class), that ultimately leads to your non-public class's public toString() method getting called. The caller doesn't know anything about your class, other than that it extends Object and therefore has a public toString().
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8414
    
  23

midhuna peru wrote:A public class can be accessed by everything. But what about the public methods in default classes? Is there a special purpose of public modifier for methods in default class?...

The fact is that public methods are always visible; the problem is that if you can't see the class, it's often difficult to call anything. However, as was pointed out to me recently, all objects are Objects, so you can always call its methods, and if they're overridden, the correct runtime method will be run even if the type itself is not visible.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
You can also extend a package‑private class by a public class. Then those public methods will be visible everywhere.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Can i get an example of how to access public method of non public class. A small code.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

midhuna peru wrote:Can i get an example of how to access public method of non public class. A small code.


I already gave you two examples:

1. Any time you call iterator() on an ArrayList or LinkedList (probably other collections as well), you're getting a private class that implements Iterator, and you can call its public methods (hasNext(), next(), toString(), hashCode(), etc.).

2. Create a non-public class that extends Object--in other words, any non-public class at all. Write a public method that's declared to return Object, and returns an instance of this class. Pass that object to System.out.println(), or even just call its toString(), hashCode(), etc. methods directly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public methods in default class