aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is need of declaring a method of a class (having default access) as public ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is need of declaring a method of a class (having default access) as public ?" Watch "what is need of declaring a method of a class (having default access) as public ?" New topic
Author

what is need of declaring a method of a class (having default access) as public ?

Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

if we have a class as follows :

class A // default access
{

public void print() // public method
{
// some code
}

}

Here class A is having default access and the method print() of it is having public declaration. Is there any meaning to this?


Thanks & Regards
Prashanth

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

The class can have a public subclass in the same package, and the inherited method can then be accessed from outside the package. Quick example (each class in its own file)If the method is given default access (by removing the public keyword) that won't compile.


luck, db
There are no new questions, but there may be new answers.
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

Thank you...

you gave me a nice explanation.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Darryl Burke wrote:The class can ... won't compile.


Thanks for the nice explanation.


SCJP 6. Learning more now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what is need of declaring a method of a class (having default access) as public ?