Search code examples
javaandroidoopclass-design

Declare Class Attribute Protected or Public?


I have a class say within a package com.practise.mypackageone.MyClass

Class MyClass has a method

 /* Modifier  */ void show()
{
 // some code here
}

I want this method to be only accessible from another package class say

com.practise.mypackagesecond.SecondClass

Now if I made the method public it will accessible to everywhere which I dont want. and if I made it protected then SecondClass has to extend MyClass in order to access it.

But any other package class can also extend my class to access that method.

How can I prevent that?


Solution

  • Put the classes in the same package and make the method package private(the default modifier). Otherwise I think what you want is not achievable.