• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

public methods in default class

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also extend a package‑private class by a public class. Then those public methods will be visible everywhere.
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i get an example of how to access public method of non public class. A small code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic